我基于分隔符(使用Boost Tokenizer)对字符串进行标记,然后根据状态变量的值对标记进行处理。
我遇到的问题:其中一个案例要求字符串进一步标记化。这会导致错误,因为在这种情况下声明了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...
}
}
答案 0 :(得分: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...
}
}
这对我有用 - 好吧它编译和标记......注意我的例子不像你的那样,迭代器的增量会在结束前导致崩溃,因为我没有做任何检查...... ..
也许你没有使用模板或错过了它?
答案 1 :(得分:0)
尝试将“{}”放在新的堆栈变量周围,如下所示,
case FIRST_STATE:
{
lineToken++;
tokenizer tok1(*lineToken, sep2);
tokenizer::iterator token=tok1.begin();
}
break;