C++ Boost正则表达式运行时错误

C++ Boost正则表达式运行时错误,c++,regex,boost,C++,Regex,Boost,我正在尝试使用我在另一台计算机上编写的一些代码,将字符串拆分为令牌。这段代码编译得很好。该代码在其他一些计算机上也能正常工作。我已设法将代码缩减为以下内容: #include <string> #include <boost/regex.hpp> typedef std::vector<std::string> token_t ; token_t generate_tokens(std::string raw_input){ //this functio

我正在尝试使用我在另一台计算机上编写的一些代码,将字符串拆分为令牌。这段代码编译得很好。该代码在其他一些计算机上也能正常工作。我已设法将代码缩减为以下内容:

#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;
}
#包括
#包括
typedef std::向量标记;
令牌\u t生成令牌(std::字符串原始\u输入){
//这个函数将一个输入字符串分解成标记。因此,test100将转到两个标记“test”和“100”。
boost::regex re_splitter(\\s+);//这使用regex\s+查找空白字符。+查找一个或多个空白字符。
sregex_令牌迭代器iter(raw_input.begin(),raw_input.end(),re_splitter,-1);
//这会使用正则表达式re_拆分器将字符串拆分为标记(当找到标记时)。
boost::sregex_token_iterator j;//这实际上是在boost示例中,j是end的等价物。是的,我一开始也觉得很奇怪。。。
令牌t令牌向量;
无符号整数计数=0;
while(iter!=j)
{
标记向量。推回(*iter);

std::cout在gdb或类似的程序中运行它,在这些部分的开头设置一个断点,然后逐步执行,直到找到有问题的行


您得到的错误看起来像是一个无效的指针被传递到了boost库的某个地方。

因为您的代码中没有使用
shared\u ptr
,而且我看不到任何其他看起来错误的东西,它可以在其他机器上运行,我想这可能是boost.Regex中的一个bug

我打赌其他机器安装了其他版本的boost


如果我不得不猜测,我会尝试更改
std::cout,这不是一个bug,而是boost头文件的冲突

这可能是因为包含了错误的文件,或者是因为包含了错误的库(regex模块是少数需要编译的boost模块之一)

您应该通过使用-l和-I开关确保使用的文件正确,例如:

   g++ -W -Wall main.cpp $(LDFLAGS) -lboost_regex -I/data1/PROJECT_SEARCH/libsrc/boost_1_46_1

当您使用boost版本的头进行编译并使用另一个boost版本执行时,就会发生这种情况。您应该检查安装了哪个boost库以执行,以及使用哪个库进行编译。

我做了建议的更改,但遇到了完全相同的问题。好吧,这只是一个猜测。正如Swiss和我已经建议的那样:启动gdb(或任何其他调试器)并查看该assert()在何处被触发。然后从那里开始。如果它是Boost.Regex中的一个bug,您应该检查它在最新版本(我认为是1.46)中是否仍然存在,如果是,请提交一份bug报告。您使用的Boost版本是什么?该代码和输入在MSVC 2010 SP1和Boost 1.46.1中运行良好