[英]Vector Iterator not Incrementable In for loop
Alright so I know people have gone on and on about this topic, but I've searched question after question and edited my code repeatedly, and I still seem to be having this problem. 好的,所以我知道人们一直在讨论这个话题,但是我已经接连搜索了一个问题,并反复编辑了我的代码,但我似乎仍然遇到这个问题。 This piece of code is basically intended to run through a vector and compare/combine its contents.
这段代码基本上旨在运行向量并比较/组合其内容。 In order to do that, I use 'it' and 'in' to go through the vector and .erase to delete contents that have been combined into new parts of the vector.
为了做到这一点,我使用“ it”和“ in”遍历向量,并删除所有已合并到向量新部分中的内容。 Knowing I would basically need the .erase function and iterators, I used some code (the auto it stuff) from other questions on StackOverflow that seemed to work in this situation.
知道我基本上需要.erase函数和迭代器之后,我使用了StackOverflow上其他问题的一些代码(自动填充),在这种情况下似乎可以使用。 Since I'm unfamiliar with that code, I may have used it incorrectly for this situation, though I'm not sure about that.
由于我不熟悉该代码,因此尽管这种情况我不确定,但我可能未正确使用它。
Anyway, as the title indicates, I'm getting a 'vector iterator not incrementable' error. 无论如何,正如标题所示,我遇到了“向量迭代器不可递增”错误。 The program seems to be running into this error after going through the second for loop a few times.
经过第二次for循环几次后,程序似乎正在运行此错误。 I've tried a lot of things, but I just can't seem to figure out what part of my code is the real problem.
我已经尝试了很多事情,但是似乎无法弄清楚代码的哪一部分才是真正的问题。
Any help would be greatly appreciated! 任何帮助将不胜感激!
\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
Once you call erase
on a std::vector
the iterator that you're using becomes invalid. 在
std::vector
上调用erase
,您正在使用的迭代器将变得无效。 Instead the call to erase
returns a brand new iterator that you should use going forward. 相反,
erase
请求返回一个全新的迭代器,您应该继续使用该迭代器。
In your case, you're using the postfix ++ operator which will attempt to increment the iterator after it has been used by the erase
method. 在您的情况下,您使用的是postfix ++运算符,该运算符将在
erase
方法使用迭代器后尝试递增该迭代器。 At that point the iterator is invalid and so you get an error. 那时,迭代器无效,因此您会收到错误消息。
What you likely want is it = strvector.erase(it);
您可能想要的是
it = strvector.erase(it);
which removes the element at the iterator and then returns a new iterator positioned at the element after the one you erased. 这将删除迭代器上的元素,然后在您删除了该元素之后返回一个位于该元素上的新迭代器。 You don't need the additional
++
because erase
effectively did that for you. 您不需要额外的
++
因为erase
有效地为您实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.