2016-05-15 9 views
0

Ich habe eine Liste von Zeichen, die mein Programm durchläuft. Während meine for-Schleife jedes Zeichen prüft, wende ich eine Anzahl von Tests an.C++ List Iterator Aktionen neu starten, wenn List ändert

Was ich tun muss, ist, wenn einer der Tests die Liste ändert (d. H. Der Hash wird geändert), starten Sie die Tests von Anfang an erneut. Nur wenn alle Tests abgeschlossen sind, kann meine 'for' Schleife mit dem nächsten Zeichen fortgesetzt werden.

Eine Do-While-Schleife könnte möglicherweise funktionieren, aber ich habe Probleme.

Im Beispiel sollte das Ergebnis "ty" sein, nicht "ttty".

 #include <iostream> 
    #include <list> 

    using namespace std; 

    void testOne(); 
    void testTwo(); 
    void print(); 
    unsigned short calculateHash(list<char> &charList); 

    list<char> charList; 
    list<char>::iterator iter; 
    list<char>::iterator iter2; 

    int main(int argc, char *argv[]){ 

     charList.push_back('t'); 
     charList.push_back('t'); 
     charList.push_back('t'); 
     charList.push_back('t'); 
     charList.push_back('t'); 
     charList.push_back('x'); 

     print(); 
     cout << "Hash = " << calculateHash(charList) << '\n'; 

     for(iter = charList.begin(), iter2 = ++charList.begin(); iter != charList.end(); ++iter, ++iter2) { 


      unsigned short hash; 
      hash = calculateHash(charList); 

      // if one of the tests changes the list 
      // start the tests again... 

      //while (hash == calculateHash(charList)) 

      // loop here. 
      testOne(); 
      testTwo(); 

     } 
     print(); 
     cout << "Hash = " << calculateHash(charList) << '\n'; 
    } 

    void testOne() { 
     if (*iter == *iter2) { 
      charList.erase(iter2); 
      iter2 = iter; 
      ++iter2; 
     } 
    }; 

    void testTwo() { 
     if (*iter == 'x') 
      (*iter) = 'y'; 

    }; 

    void print() { 
     list<char>::iterator it; 
     for(it = charList.begin(); it != charList.end(); it++) 
      cout << *it; 

     cout << '\n'; 
    }; 

    unsigned short calculateHash(list<char> &charList) { 
     unsigned short shift, hash = 0; 
     list<char>::const_iterator itr; 
     for (itr = charList.begin(); itr != charList.end(); itr++) { 
      hash ^= *itr; 
      shift = (hash & 15); 
      hash = (hash << shift) | (hash >> (16 - shift)); 
     } 
     return hash; 
    }; 

Antwort

0

sollten Sie eine while-Schleife etwas wie diese verwenden

iter = charList.begin(); 
while (iter != charList.end()) { 
    hash = computeHash(); 
    testOne(); 
    newhash = computeHash(); 
    if (hash != newHash) { 
     iter = charList.begin(); 
     continue; 
    } 
    /* do the same for all other tests*/ 
    iter++; 
} 
0

ich es als eine for-Schleife schreiben würde:

bool reset; 
for (auto it = charList.begin(); 
    it != charList.end(); 
    it = (reset ? charList.begin() : it+1)) 
{ 
    const unsigned short hash = calculateHash(charList); 

    // loop here. 
    // Note both functions need to return true if the loop should be 
    // restarted. If testOne returns true, testTwo will not be executed 
    // (which is just as well, as the list has changed under it's feet. 
    reset = testOne() || testTwo(); 
} 
Verwandte Themen