2017-03-23 15 views
0

ベクタータイプの文字列から特定のインデックスを削除する際に問題があります。私の仕事は、htmlタグ(html、headなど)でベクトルを埋め込むことですが、一致のないタグを取り除く必要があります(p、br、imgのみと仮定)。 forループの後にベクターを印刷するたびに、私は迷子brとpを取得します。助けていただければ幸いです!特定のインデックスにあるベクトルの要素を削除する

for (int i = 0; i < tagStorage.size(); i++) { 

    if (tagStorage[i].find_first_of('/') == true) { //searches for closing tags 
     closingTags.push_back(tagStorage[i]); 
    } 
    else if (tagStorage[i].find("<p>") != string::npos) { //searches for <p> tag 
     noMatch.push_back(tagStorage[i]); 
     tagStorage.erase(tagStorage.begin() + i); //erase tag 
    } 
    else if (tagStorage[i].find("<br>") != string::npos) { //searches for <br> tag 
     noMatch.push_back(tagStorage[i]); 
     tagStorage.erase(tagStorage.begin() + i); //erase tag 
    } 
    else if (tagStorage[i].find("<img") != string::npos) { //searches for <img ..> tag 
     noMatch.push_back(tagStorage[i]); 
     tagStorage.erase(tagStorage.begin() + i); //erase tag 
    } 
    else { 
     openingTags.push_back(tagStorage[i]); 
    } 
} 
+0

http://stackoverflow.com/questions/4442477/remove-ith-item-from-a-c-stdvectorからの回答を試す –

答えて

2

現在のものを削除した後、次に予想されるtagStorage要素を反復しています。

例として、「Hello」からすべての「l」を削除したいとします。

"Hello!" 
^ 

私は

"Hello!" 
^

++を

"Hello!" 
^

++を 'L' を削除します。

あなたが見ることができるように私は

"Helo!" 
    ^

++を、「L」を除去した後の反復は、右隣の文字を過ぎて行きます。解決策は、forループのインクリメントを補償するように要素を削除した後に、iをデクリメントすることです。

関連する問題