循环的矢量迭代器不可递增In
Vector Iterator not Incrementable In for loop
好吧,我知道人们一直在谈论这个话题,但我已经搜索了一个又一个问题,并反复编辑了我的代码,我似乎仍然有这个问题。这段代码的主要目的是运行一个向量并比较/组合其内容。为了做到这一点,我使用"it"answers"In"来遍历向量,并使用.erase来删除已组合到向量的新部分中的内容。我知道我基本上需要.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
的调用返回了一个全新的迭代器,您应该继续使用它。
在您的例子中,您使用的是postfix++运算符,它将在erase
方法使用迭代器之后尝试递增迭代器。此时迭代器是无效的,因此您会得到一个错误。
您可能想要的是it = strvector.erase(it);
,它删除迭代器中的元素,然后返回位于您删除的元素之后的元素处的新迭代器。您不需要额外的++
,因为erase
有效地为您做到了这一点。
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- 我可以使用反向迭代器作为ForwardIt吗
- ESP8266单片机矢量迭代器的C++问题
- 如何在C++中将迭代器作为函数参数传递
- 是否应避免从非常量迭代器转换为常量迭代器?
- 如何在 c++ 中将字符串迭代器变量传递给函数?
- 地图和迭代器(IN)验证
- 循环的矢量迭代器不可递增In