c++ - vector 迭代器在 for 循环中不可递增

标签 c++ vector

好吧,我知道人们一直在谈论这个话题,但我一个接一个地搜索问题并反复编辑我的代码,但我似乎仍然遇到这个问题。 这段代码基本上是为了遍历一个 vector 并比较/组合它的内容。为此,我使用“it”和“in”遍历 vector ,并使用 .erase 删除已组合到 vector 新部分中的内容。知道我基本上需要 .erase 函数和迭代器,我使用了 StackOverflow 上其他问题的一些代码(auto it 东西),这些代码似乎在这种情况下有效。由于我不熟悉该代码,因此我可能在这种情况下错误地使用了它,尽管我对此不确定。

无论如何,如标题所示,我遇到了“vector 迭代器不可递增”错误。在经过几次第二个 for 循环后,程序似乎遇到了这个错误。我已经尝试了很多东西,但我似乎无法弄清楚我的代码的哪一部分是真正的问题。

如有任何帮助,我们将不胜感激!

\if (!strvector.empty()) {
        for (auto in = strvector.begin(); in != strvector.end() - 1;) {//in order to process if there is more than one final piece

            str = ' ' + *in + ' ';//adds spaces so it only compares beginnings & ends
            if (strvector.size() < 2) {
                break;
            }//doesn't do anything if there are too few objects to process

            for (auto it = strvector.begin(); it != strvector.end() - 1;) { 
                if (strvector.size() < 2) {
                    break;
                }//doesn't continue if there are too few objects to process
                str2 = ' ' + *it + ' '; //adds spaces so it only compares beginnings & ends
                substr = str; //sets substring of string to be chopped up
                while (substr.length() >= 6) { //only processes substrings >= 6 characters bc smaller bits are useless
                    size_t found = str2.find(substr); //searches str2 for substr
                    if (found != string::npos) {
                        str = str.substr(0, substr.length()) + ' '; //cuts substr off of str
                        str.append(str2); //adds str and str2
                        it = strvector.erase(it);
                        substr = 'a'; //shortens substr to get out of while
                        test=1;
                    }//end if
                    else {
                        substr.erase(substr.size() - 1, 1); //if substr was not found, decrement substr and compare again
                    }
                }//end while

                substr = str; //resets substr
                while (substr.length() >= 6) { //only processes substrings >= 6 characters bc smaller bits are useless
                    size_t found = str2.find(substr); //searches str2 for substr
                    if (found != string::npos) {
                        str = str.substr(substr.length()) + ' '; //cuts substr from beginning of string
                        str = str2 + str; //adds str2 and str, with str2 at the beginning
                        it = strvector.erase(it++);
                        substr = 'a'; //shortens substr to get out of while 
                        test=1;

                    }//end if
                    else {
                        substr.erase(0, 1); //erases the first character of the string
                    }
                    if (test < 1) {
                        it++; //increments if the erase function did not already do that
                    }

                }//end while
                if (test != 1) {
                    it++; //increments if the erase function did not already do that
                }
                if (test < 2) {
                    strvector.push_back(str); //adds new str to the vector
                    test = 0;
                }
            }//end ptr2 for
            if (strvector.size() < 2) {
                in = strvector.erase(in - 1);
            }
            else {
                in++;
            }
            cout << "str1 is " << str << endl; //prints out str
        }//end ptr for
    }//end if vector is not empty

最佳答案

一旦您在 std::vector 上调用了 erase,您正在使用的迭代器就会变得无效。相反,对 erase 的调用会返回一个全新的迭代器,您应该继续使用它。

在您的例子中,您使用的是后缀++ 运算符,它将在迭代器被 erase 方法使用后尝试递增迭代器。此时迭代器无效,因此您会收到错误消息。

您可能想要的是 it = strvector.erase(it);,它会删除迭代器中的元素,然后返回位于您删除的元素之后的元素的新迭代器。您不需要额外的 ++,因为 erase 可以有效地为您完成。

关于c++ - vector 迭代器在 for 循环中不可递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28847067/

相关文章:

C++:从 union 中检索值的模板函数

c++ - 在 C++ 中迭代的优雅方式

java - 为什么 java.util.Stack 是使用 Vector 而不是 Arraylist 实现的

C++ |将 int 转换为 byte[4],反之亦然

C++ 内联转换和转换为变量的工作方式不同

r - 如何在R中求两个不相等向量的值之和?

c++ - 如何用字符串文字优雅地初始化 vector<char *>?

r - (R 统计包)对于向量中的每个值,计算该值在不同向量中出现的次数

c++ - 多平台C++项目结构

C++:需要从 dll 发出 HTTP 请求 - 我从哪里开始?