c++ - 将 boost std::string::const_iterator 转换为 string 和 int c++

标签 c++ regex boost casting

我需要将 const_iterator 转换为我可以对其执行 int 和 string 操作的类型。 results[1] 包含我需要处理的来自 regex_search 的文本。 我已经花了几个小时尝试转换为可行的格式,但没有成功...

boost::match_results<std::string::const_iterator> results;
      boost::regex ex(pattern, boost::regex::perl);
      if(boost::regex_search(line, results, ex))
        (results[1] > 10) ? cout << "Fail" : cout << "Pass";

谢谢, 乔

最佳答案

您可以从 const 字符串迭代器的匹配结果中创建一个 std::string,如下所示:

std::string result_string( results[1].first, results[1].second );

...或者简单地说:

std::string result_string = results[1].str();

您可以像这样将字符串强制转换为 int:

int result_int = boost::lexical_cast< int >( result_string );

...或者,如 @Jonathan Wakely 所指出的,如果您使用的是 C++11:

int result_int = std::stoi( result_string );

关于c++ - 将 boost std::string::const_iterator 转换为 string 和 int c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13959466/

相关文章:

c++ - 默认构造函数编译错误,预期的ID不合格

c++ - 了解 OpenGL 中的绑定(bind)?

javascript - 在 .split() 函数中使用捕获组

php - 您如何编写和测试正则表达式?

c++ max_element 每n个元素

c++ - 作为标准,C++ 是否禁止在单个类实例中存储成员函数?

c++ - 将两个字符的字符串变量转换为十六进制

c# - .NET 正则表达式使用反向引用替换

C++ boost::bind 说不可访问的基类

c++ - 为什么使用openmp时会间歇性出现 “fatal error C1001”错误?