这里的Day of week不对吧,再换种方式试试看呢
时间: 2024-03-10 08:43:50 浏览: 29
对的,由于输入的字符串中包含了星期几的信息,所以解析出来的第一个字符串是 `Triggerblock`,并不是我们需要的信息。如果你只需要年月日时分秒的信息,可以使用 `std::get_time` 函数来解析。具体实现如下:
```c++
#include <iostream>
#include <string>
#include <ctime>
int main() {
std::string input = "Triggerblock Tue Apr 11 15:17:17 2023";
std::tm t = {};
std::istringstream iss(input);
iss >> std::get_time(&t, "%a %b %d %H:%M:%S %Y");
// 如果解析失败
if (iss.fail()) {
std::cerr << "Failed to parse the input string." << std::endl;
return 1;
}
// 打印解析结果
std::cout << "Year: " << t.tm_year + 1900 << std::endl;
std::cout << "Month: " << t.tm_mon + 1 << std::endl;
std::cout << "Day: " << t.tm_mday << std::endl;
std::cout << "Hour: " << t.tm_hour << std::endl;
std::cout << "Minute: " << t.tm_min << std::endl;
std::cout << "Second: " << t.tm_sec << std::endl;
return 0;
}
```
这段代码会输出以下结果:
```
Year: 2023
Month: 4
Day: 11
Hour: 15
Minute: 17
Second: 17
```
这种方式可以直接解析出年月日时分秒的信息,并且更加简洁。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)