2012-10-25 36 views
0

我基於分隔符(使用Boost Tokenizer)來標記一個字符串,然後根據狀態變量的值做標記。Tokenizing一個字符串內的開關案例

我遇到的問題:其中一種情況需要將字符串進一步標記。這會導致錯誤,因爲tok1和令牌迭代器變量在大小寫中聲明。我試過在交換機之外聲明它們,只是將它們分配給外殼,但這不起作用。

有沒有人知道我如何使這項工作或如果有更好的方式去進一步標記案件中的字符串?謝謝!

如下代碼示例:

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... 
    } 

} 
+1

是一個'if-statement'選項嗎? – andre

回答

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... 
} 
} 

這對我的工作 - 以及它編譯和....標記化請注意,我的例子是不喜歡你的迭代器的增量將在結束前導致崩潰,因爲我沒有做任何檢查.....

也許你沒有使用該模板或錯過了它?

0

嘗試把 「{}」,圍繞新的堆棧變量,這樣的事情,

case FIRST_STATE: 

{

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

}

break;