2014-11-23 82 views
1
string zodis; 
sregex_token_iterator it(eil.begin(), eil.end(), std::regex("[A-Za-z]+")); 
sregex_token_iterator reg_end; 

for (; it != reg_end; ++it) { 
    zodis = it -> str(); 
    /* ... */ 
} 

基本上我得到使用上面的代碼字查找的詞,但我想在什麼位置字符串的zodis先來了解一下字符字符串eil的是(字符串zodiseil拍攝)使用std :: regex_token_iterator(不necessarilly)找到字符串中的字符位置

我試圖指向迭代器begin()回來,但只是給了我字符

任何想法?

+0

'std :: string :: find'。 – Aleph 2014-11-23 11:29:48

+0

如果我有超過1個相同的單詞,我該如何找到我需要的單詞,而不是後面的單詞,我將不得不經過每個單詞,以及如果單詞在他們之前和之後有不同的字符會怎樣?基本上就像「// one()(-one))」,它仍然可以工作嗎? – 2014-11-23 11:31:56

+0

看看http://en.cppreference.com/w/cpp/string/basic_string/find,特別是'pos'參數。 – Aleph 2014-11-23 11:32:59

回答

0

沒關係,所以我想出了一個辦法乾淨地做到這一點。

int basepoint=0; 
    while(whatever) 
    { 
     int c1=zodis.end()-zodis.begin();//the length of the word 
     int c2=eil.find(zodis,basepoint);//the starting position of the word 
     basepoint+=c1+c2;//start point for the find function for the next loop 
    } 

這會發現所有的字開始點(即使整條生產線是同一個詞)

如果您在使用本

cout << c1 << " " << c2; 
過的

word word word word word 

文本文件

其中c1是取自字符串長度的單詞,c2是起始位置, 會給你

4 0 
4 5 
4 10 
4 15 
4 20