我有两个字符串 srt1 和 str2 作为:
std::string str1 = "20110627120000";
std::string str2 = "20110629120000";
我需要的只是将它们转换为时间格式并比较哪个更大。我正在使用下面的代码,但我遇到了分段错误:
tm tm1,tm2;
sscanf(str1.c_str(),"%4d%2d%2d %2d%2d%2d",&tm1.tm_year,&tm1.tm_mon,&tm1.tm_mday,&tm1.tm_hour,&tm1.tm_min,&tm1.tm_sec);
sscanf(str2.c_str(),"%4d%2d%2d %2d%2d%2d",&tm2.tm_year,&tm2.tm_mon,&tm2.tm_mday,&tm2.tm_hour,&tm2.tm_min,&tm2.tm_sec);
std::cout << "5 \n";
if ((tm1.tm_year < tm2.tm_year) && (tm1.tm_mon<tm2.tm_mon) && (tm1.tm_mday<tm2.tm_mday ))
{
std::cout << str2 <<"is greater \n";
}