C++ 从字符串中标记/提取信息的最佳方法

C++ 从字符串中标记/提取信息的最佳方法,c++,string,datetime,tokenize,C++,String,Datetime,Tokenize,我试图将接收到的日期时间转换为特定格式,以插入MySQL数据库。这个程序是用C++编写的,下面的解决方法起作用,但是我觉得它效率很低。 输入为:周一11月08日17:41:23+0000 2010 所需的输出格式为:YYYY-MM-DD HH:MM:SS 对于这个例子,输出是:2010-11-0817:41:23 我已经包括了代码的相关部分 //class variable std::map<std::string, std::string> monthMap; v

我试图将接收到的日期时间转换为特定格式,以插入MySQL数据库。这个程序是用C++编写的,下面的解决方法起作用,但是我觉得它效率很低。 输入为:周一11月08日17:41:23+0000 2010
所需的输出格式为:YYYY-MM-DD HH:MM:SS
对于这个例子,输出是:2010-11-0817:41:23

我已经包括了代码的相关部分

    //class variable
    std::map<std::string, std::string> monthMap;

void processor::initializeMonthMap(){
    monthMap["Jan"] = "01";
    monthMap["Feb"] = "02";
    monthMap["Mar"] = "03";
    monthMap["Apr"] = "04";
    monthMap["May"] = "05";
    monthMap["Jun"] = "06";
    monthMap["June"] = "06";
    monthMap["Jul"] = "07";
    monthMap["July"] = "07";
    monthMap["Aug"] = "08";
    monthMap["Sept"] = "09";
    monthMap["Sep"] = "09";
    monthMap["Oct"] = "10";
    monthMap["Nov"] = "11";
    monthMap["Dec"] = "12";
}

inline std::string processor::convertDate(std::string input) {
    //Format: Mon Nov 08 17:41:23 +0000 2010
    //To get to YYYY-MM-DD HH:MM:SS

    std::stringstream  newString(input);
    std::string temp1;
    std::string temp2;

    // Read Day in txt, discard
    newString >> temp1;

    //Read month, convert to number
    newString >> temp1;
    temp2 = "-" + monthMap[temp1] + "-";

    //Read Day in number
    newString >> temp1;
    temp2.append(temp1 + " ");

    //Read TimeStamp
    newString >> temp1;
    temp2.append(temp1);

    //Discard UTM adjustment
    newString >> temp1;

    //Read year
    newString >> temp1;

    //Add year to beginning of input
    temp1.append(temp2);

    return temp1;
}
//类变量
地图月地图;
无效处理器::initializeMonthMap(){
月地图[“一月”]=“01”;
月地图[“二月”]=“02”;
月地图[“三月”]=“03”;
月地图[“四月”]=“04”;
月地图[“五月”]=“05”;
月地图[“六月”]=“06”;
月地图[“六月”]=“06”;
monthMap[“Jul”]=“07”;
月地图[“七月”]=“07”;
月地图[“八月”]=“08”;
月地图[“九月”]=“09”;
月地图[“九月”]=“09”;
月地图[“十月”]=“10”;
月地图[“11”]=“11”;
月地图[“十二月”]=“12”;
}
内联std::字符串处理器::转换日期(std::字符串输入){
//格式:2010年11月8日星期一17:41:23+0000
//要到达YYYY-MM-DD HH:MM:SS
std::stringstream新闻字符串(输入);
std::字符串temp1;
std::字符串temp2;
//在txt中读取日期,放弃
新闻字符串>>temp1;
//读取月份,转换为数字
新闻字符串>>temp1;
temp2=“-”+月地图[temp1]+“-”;
//数日阅读
新闻字符串>>temp1;
temp2.追加(temp1+“”);
//读取时间戳
新闻字符串>>temp1;
temp2.append(temp1);
//放弃UTM调整
新闻字符串>>temp1;
//阅读年
新闻字符串>>temp1;
//将年份添加到输入的开始
temp1.append(temp2);
返回temp1;
}

如果您使用的是POSIX系统(即非windows),则可以使用strTime:

这将获取一个字符串并构建一个
struct tm
,您可以通过strftime以任何所需格式输出它


有一些windows实现,但默认情况下不可用。请参阅此处的更多信息:

使用。您可以使用格式字符串以多种方式格式化输入和输出。

您正在解析字符串,幸运的是,您可以通过简单的附录构建输出。我不认为这会更有效率

但是,似乎可以使用seekg将光标定位到一周中的最后一天,并进行UTM调整


您可以使用正则表达式(std::tr1或boost::regex)作为干净的解决方案。如果你的格式是标准格式,你也可以查看Boost.Date\u Time。你有没有衡量过编译器优化代码的运行情况?您的代码看起来非常简单。你可以挤出更多,但值得吗?像您这样干净、易懂且合理高效的代码非常有价值。(如果你只是把tempXXX改名为合理的名字…;)总的来说,我同意@Peter。但在这种情况下,使用日期/时间库,这段代码基本上可以简化为两行:
get\u-date\u-from\u-string(…,format)
,然后是
get\u-string\u-from\u-date(…,other\u-format)
。这将更加清晰和简洁。提到的其他方法可能会减少代码,但不会提高效率。感谢提供的信息。我确实有一个关于stringstream的问题。若我每次都使用string.find()而不是构建一个stringstream,你们认为我会做得更好吗?若你们知道不同部分的长度总是相同的(看起来是一样的)。如果您想放弃stringstream,我将使用std::string::substr(start,count)。