Boost regex运行时错误 [英] Boost regex runtime error
问题描述
我试图使用我在另一台计算机上编写的将字符串拆分为令牌的代码。这段代码编译得很好。该代码还在其他计算机上工作。我已经设法将代码减少到以下:
#include< string>
#include< boost / regex.hpp>
typedef std :: vector< std :: string> token_t;
token_t generate_tokens(std :: string raw_input){
//此函数将输入字符串拆分为标记。所以测试100进入2个令牌测试和100。
boost :: regex re_splitter(\\s +); //这使用正则表达式\s +来查找空格。 +查找一个或多个空格字符。
boost :: sregex_token_iterator iter(raw_input.begin(),raw_input.end(),re_splitter,-1);
//这会打破字符串使用regex re_splitter分裂成令牌时,找到。
boost :: sregex_token_iterator j; //这实际上是在Boost的例子中,j是等效的end。是的,这也似乎对我来说很奇怪...
token_t token_vector;
unsigned int count = 0;
while(iter!= j)
{
token_vector.push_back(* iter);
std :: cout<< * iter ++<< std :: endl;
++ count;
}
return token_vector;
}
int main(){
std :: string in;
int amount = -1;
std :: cout<< action:;
std :: getline(std :: cin,in);
boost :: regex EXPR(^ test \\d *(\\.\\d {1,2})?$);
bool format_matches = boost :: regex_match(in,EXPR);
token_t tokens = generate_tokens(in);
if(format_matches){
amount = atoi(tokens.at(1).c_str());
}
std :: cout<< amount:<量< \\\
;
return 0;
}
使用以下命令编译没有错误或警告: g ++ - Wall test.cpp -lboost_regex
但是在运行时使用时提供输入 test 100
程序失败。
action:test 100
a.out:/ usr / local / include / boost / smart_ptr / shared_ptr.hpp:412:typename boost :: detail :: shared_ptr_traits :: reference boost :: shared_ptr :: operator *()const [with T = boost :: regex_traits_wrapper>>]:Assertion`px!= 0'failed。 / p>
已中止
。这是一个错误在我的代码或在库中?
由于您不使用 shared_ptr
在代码中,我可以看到没有其他的东西看起来错误,它在其他机器上工作,我会说这可能是Boost.Regex的一个错误。
我敢打赌其他机器都安装了其他版本的boost?
如果我不得不猜测,我会尝试更改 std :: cout < * iter ++<< std :: endl;
行首。 - > std :: cout<< * iter<< std :: endl; ++ iter;
。
然后,在像Swiss建议的调试器中运行它,看看断言触发的地方。 >
I'm trying to use some code that I wrote on another computer that splits a string into tokens. This code compiles fine. The code also works as intended on some other computers. I've managed to reduce the code down to the following:
#include <string>
#include <boost/regex.hpp>
typedef std::vector<std::string> token_t ;
token_t generate_tokens(std::string raw_input){
//this function breaks a input string into tokens. So test 100 goes to 2 tokens "test" and "100".
boost::regex re_splitter("\\s+"); //this uses the regex \s+ to find whitespace. The + finds one or more whitespace characters.
boost::sregex_token_iterator iter(raw_input.begin(), raw_input.end(), re_splitter, -1);
//this breaks the string using the regex re_splitter to split into tokens when that is found.
boost::sregex_token_iterator j; //This is actually in the Boost examples, j is the equivalent of end. Yes this did also seem weird to me at first...
token_t token_vector;
unsigned int count = 0;
while(iter != j)
{
token_vector.push_back(*iter);
std::cout << *iter++ << std::endl;
++count;
}
return token_vector;
}
int main(){
std::string in;
int amount = -1;
std::cout << "action: ";
std::getline(std::cin, in);
boost::regex EXPR("^test \\d*(\\.\\d{1,2})?$");
bool format_matches = boost::regex_match(in, EXPR);
token_t tokens = generate_tokens(in);
if(format_matches){
amount = atoi(tokens.at(1).c_str());
}
std::cout << "amount: " << amount << "\n";
return 0;
}
This compiles without errors or warnings using: g++ -Wall test.cpp -lboost_regex
but when used at runtime providing the input test 100
the program fails.
action: test 100
a.out: /usr/local/include/boost/smart_ptr/shared_ptr.hpp:412: typename boost::detail::shared_ptr_traits::reference boost::shared_ptr::operator*() const [with T = boost::regex_traits_wrapper > >]: Assertion `px != 0' failed.
Aborted
I'm completely lost as to what is going on here. Is this a bug in in my code or in the library? Any advice for debugging this is greatly appreciated!
Since you're not using shared_ptr
in your code, and I can see no other stuff that looks wrong AND it works on other machines, I'd say it's probably a bug in Boost.Regex.
I bet the other machines have other versions of boost installed?
If I had to guess I'd try changing the std::cout << *iter++ << std::endl;
line first. -> std::cout << *iter << std::endl; ++iter;
.
And yeah, run it in a debugger like Swiss suggested, and see where the assertion is triggered.
这篇关于Boost regex运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!