1

我正在研究一个简单的 NLP 项目,它给定一个字符串,将确定各种参数。

给定以下输入:

07122012 12102012

代码:

string REGEX_DATE = "((\\d{2})/(\\d{2})/(\\d{4}))";

regex expressionFormat(REGEX_DATE);

sregex_token_iterator i(input.begin(), input.end(), expressionFormat, 1);

sregex_token_iterator j;

while(i != j)
{
result = *i++;
}

存储和比较结果的最佳方式是什么?(确定哪个日期更早)

4

1 回答 1

1

最好的方法是构造和比较日期而不是字符串或数字:

#include <iostream>
#include <string>
#include <boost/date_time.hpp>

int main()
{
    std::string input = "07122012 12102012";

    std::istringstream buf(input);
    buf.imbue(std::locale(buf.getloc(),
              new boost::posix_time::time_input_facet("%d%m%Y")));

    boost::posix_time::ptime d1, d2;
    buf >> d1 >> d2;

    if(d1 < d2)
        std::cout << d1 << " before " << d2 << '\n';
    else
        std::cout << d2 << " before " << d1 << '\n';
}

在线演示: http: //liveworkspace.org/code/989ba879e622aed7866e7dba2d0f02ee

于 2012-10-16T02:39:02.197 回答