【问题标题】:C++ List Iterator Restart actions if List changesC++ List Iterator 如果 List 更改则重新启动操作
【发布时间】:2016-05-15 07:02:00
【问题描述】:

我有我的程序迭代的字符列表。当我的 for 循环检查每个字符时,我应用了许多测试。

我需要做的是,如果其中一项测试更改了列表(即哈希更改),请从头开始重新启动测试。只有完成所有测试,我的“for”循环才能继续到下一个字符。

do-while 循环可能会起作用,但我遇到了麻烦。

在示例中,结果应该是“ty”,而不是“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;
     };

【问题讨论】:

    标签: c++ list hash iterator


    【解决方案1】:

    你应该使用类似这样的while循环

    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++;
    }
    

    【讨论】:

      【解决方案2】:

      我会把它写成一个 for 循环:

      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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        • 2018-08-23
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 2020-10-25
        相关资源
        最近更新 更多