这里的月是英文,我想把它转换成数字的形式
时间: 2024-03-03 16:47:22 浏览: 23
如果你想将英文月份转换为数字形式,可以定义一个数组,将月份的英文名称与数字对应起来。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
#include <sstream>
#include <map>
int main() {
std::string target = "Begin TriggerBlock Tue Feb 07 15:52:38 PM 2023";
std::stringstream ss(target);
std::string begin, trigger_block, day_of_week, month_str, day, time, meridian, year;
ss >> begin >> trigger_block >> day_of_week >> month_str >> day >> time >> meridian >> year;
// 将英文月份转换为数字
std::map<std::string, int> month_map = {
{"Jan", 1}, {"Feb", 2}, {"Mar", 3}, {"Apr", 4},
{"May", 5}, {"Jun", 6}, {"Jul", 7}, {"Aug", 8},
{"Sep", 9}, {"Oct", 10}, {"Nov", 11}, {"Dec", 12}
};
int month = month_map[month_str];
int hour, minute, second;
std::sscanf(time.c_str(), "%d:%d:%d", &hour, &minute, &second);
std::cout << "Year: " << year << std::endl;
std::cout << "Month: " << month << std::endl;
std::cout << "Day: " << day << std::endl;
std::cout << "Hour: " << (meridian == "PM" ? hour + 12 : hour) << std::endl;
std::cout << "Minute: " << minute << std::endl;
std::cout << "Second: " << second << std::endl;
return 0;
}
```
这个程序中,我们使用了一个 `std::map` 容器,将英文月份和对应的数字进行了映射。在解析字符串时,我们先将英文月份存放到 `month_str` 变量中,然后使用 `month_map` 查找对应的数字。注意,这里的 `month` 变量类型为 `int`,表示月份的数字形式。
最后,我们将解析结果输出到控制台上,包括年、月、日、小时、分钟和秒。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)