0

我正在基于分隔符(使用 Boost Tokenizer)对字符串进行标记,然后根据状态变量的值对标记进行处理。

我遇到的问题:其中一种情况需要进一步标记字符串。这会导致错误,因为在 case 中声明了 tok1 和 token 迭代器变量。我试过在开关之外声明它们,然后在机箱内分配它们,但这不起作用。

有谁知道我可以如何完成这项工作,或者是否有更好的方法来进一步标记案例内的字符串?谢谢!

下面的示例代码:

 boost::char_separator<char> sep(TOKEN_DELIMETER_NEWLINE);                  
 boost::char_separator<char> sep2(TOKEN_DELIMETER_SPACE);                  
 tokenizer tok(_text, sep);

while(lineToken!=tok.end())
{
    switch(state)
    {
        case FIRST_STATE:

            lineToken++;
            tokenizer tok1(*lineToken, sep2);
            tokenizer::iterator token=tok1.begin();  

        break;
    // Other Cases follow...
    }

}
4

2 回答 2

1

填补空白后,我编译了以下内容:

std::string _text1 = "The rain,In Spain,Lies Mainly,On the plain";

boost::char_separator<char> sep(",");                  
boost::char_separator<char> sep2(" ");                  
boost::tokenizer<boost::char_separator<char>> tok(_text1,sep);
boost::tokenizer<boost::char_separator<char>>::iterator lineToken = tok.begin();
unsigned int state = 0;

while(lineToken!=tok.end())
{
    switch(state)
    {
    case 0:
     lineToken++;
     boost::tokenizer<boost::char_separator<char>> tok1(*lineToken, sep2);
     boost::tokenizer<boost::char_separator<char>>::iterator token=tok1.begin();  

    break;
    // Other Cases follow...
}
}

哪个对我有用-它可以编译和标记化....请注意,我的示例与您的示例不同,因为迭代器的增量将在结束前导致崩溃,因为我不进行任何检查.....

也许您没有使用模板或错过了它?

于 2012-10-25T22:52:07.213 回答
0

尝试将“{}”放在新的堆栈变量周围,就像这样,

    case FIRST_STATE:

{

        lineToken++;
        tokenizer tok1(*lineToken, sep2);
        tokenizer::iterator token=tok1.begin();  

}

    break;
于 2012-10-25T22:11:05.293 回答