gpt4 book ai didi

c++ - boost 正则表达式错误:未初始化的 boost::match_results

转载 作者:行者123 更新时间:2023-11-28 07:02:52 24 4
gpt4 key购买 nike

运行时

    bool my_compare(const std::string& string_1, const std::string& string_2)
{
const boost::regex str_re("so(m)e_(r)e(g)ex");
boost::smatch str_match;

size_t one;
uint8_t two;
size_t three;
if( boost::regex_search(string_1, str_match, str_re) ) {
one = boost::lexical_cast<size_t>( std::string(str_match[1].first, match[1].second) );
two = boost::lexical_cast<uint8_t>( std::string(str_match[2].first, match[2].second) );
three = boost::lexical_cast<size_t>( std::string(str_match[3].first, match[3].second) );
}
size_t four;
uint8_t five;
size_t six;
if( boost::regex_search(string_1, str_match, str_re) ) {
four = boost::lexical_cast<size_t>( std::string(str_match[1].first, match[1].second) );
five = boost::lexical_cast<uint8_t>( std::string(str_match[2].first, match[2].second) );
six = boost::lexical_cast<size_t>( std::string(str_match[3].first, match[3].second) );
}
return false;
}

int main()
{
my_compare(some_string1, some_string2);
return 0;
}

我收到以下错误,我不明白:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl

' what(): bad lexical cast: source type value could not be interpreted as target

最佳答案

"so(m)e_(r)e(g)ex"

此正则表达式将使用字符串 "m" 填充捕获 str_match[1],使用字符串 填充 str_match[2] "r"str_match[3] 与字符串 "g"

 boost::lexical_cast<size_t>( std::string(str_match[1].first, match[1].second) );

这将尝试将字符串 "m" 转换为 size_t 类型的值,但失败("m"不是整数!),并抛出异常

' what(): bad lexical cast: source type value could not be interpreted as target

(顺便说一句,你不需要用一对迭代器中的字符串构造函数跳过这些步骤,子匹配可以直接转换为字符串)

关于c++ - boost 正则表达式错误:未初始化的 boost::match_results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com