这个错误提示:迭代器不可以增加

exmaple:

vector<int> tVecInt;
vector<int>::reverse_iterator iterInt = tVecInt.rbegin();
cout << *iterInt << endl;
system("pause");
return 0;

运行结果:

这个很明显是由于迭代器越界访问导致崩溃的;

一个比较不易看出的例子:

vector<int> tVecInt = {1,2,3,4,5};
vector<int>::iterator iterInt = tVecInt.begin();
for (; iterInt < tVecInt.end(); ++iterInt) {
if ((*iterInt) == 3)
tVecInt.erase(iterInt);
cout << *iterInt << endl;
}
system("pause");
return 0;

运行结果:

原因:当一个容器执行了一次earse操作之后,原来用来遍历的iterator就失效了,其行为是不可预测的,具体情况由实现决定。同时earse操作会返回一个指向container下一个元素的iterator,如果想继续遍历,就得用返回的iterator继续操作。

更好的代码写法(参考C++ Primer第五版 312页)

vector<int> tVecInt = {1,2,3,4,5};
vector<int>::iterator iterInt = tVecInt.begin();
while (iterInt!=tVecInt.end())
{
if ((*iterInt) == 3)
iterInt = tVecInt.erase(iterInt);
else
++iterInt;
}
system("pause");
return 0;

如此,那个错误就不再出现了。很多代码写法是有理由的,好的代码可以更简洁,清晰易懂,出错可能性更低,更易维护

ERROR: iterator not incrementable || iterator not decrementable的更多相关文章

  1. vector iterator not incrementable For information on how your program can cause an an assertion Failure, see the Visual c + + documentation on asserts

    #include <list> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { list<int> sl ...

  2. VC++ 迭代器 iterator, const_iterator, const iterator

    迭代器 iterator, const_iterator, const iterator 迭代器iterator的作用类似于指针. (1)iterator只有针对制定<类型>的容器才有效. ...

  3. [转]vector iterator not incrementable 的问题

    转自:http://blog.csdn.net/kuaile123/article/details/11105115 vector::erase误使用问题: 暂时使用经验: 不能在循环中使用,否则会报 ...

  4. map set iterator not incrementable 解决办法

    例子: #include <iostream> #include <map> using namespace std; int main() { map<int, int ...

  5. java.lang.NoSuchMethodError: org.json.JSONArray.iterator()Ljava/util/Iterator 阿里云短信

    请尝试使用 <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk ...

  6. javascript设计模式-迭代器模式(Iterator)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. c++迭代器(iterator)详解

    1. 迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型.(1) 每种容器类型都定义了自己的迭代器类型,如vector:vector<int>::iterator iter ...

  8. Peeking Iterator 解答

    Question Given an Iterator class interface with methods: next() and hasNext(), design and implement ...

  9. 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录

    1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误 解决办法很简单,不能让list为空 2 刚进入该界面的时候list是有数据的,当点击提 ...

随机推荐

  1. eclipse配置maven后无法下载jar

    1.检查网络拦截是否正常 2.进入maven依赖库根目录搜索出该目录下的*lastUpdated.properties文件并删除 然后就可以继续下载jar包了

  2. 查找Python项目依赖的库并生成requirements.txt

    使用pip freeze pip freeze > requirements.txt 这种方式配合virtualenv 才好使,否则把整个环境中的包都列出来了. 使用 pipreqs 这个工具的 ...

  3. 关于sdk>=23的android版本权限的问题

    在SDK23也就是Android6.0.1里编写调用系统通讯录读写权限的程序,在AndroidManifest.xml中,已经配置了 <uses-permission android:name= ...

  4. String[]字符串数组,按字典顺序排列大小

    package ltb6w1; public class WordSort1 { private String[] a= {"hello","world",&q ...

  5. 项目出现 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法

    1. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path ①项 ...

  6. 饿了么移动APP的架构演进

    1MVC 我们常说,脱离业务谈架构就是纯粹的耍流氓.饿了么移动APP的发展也是其业务发展的一面镜子. 在饿了么业务发展的早期,移动APP经历了从无到有的阶段.为了快速上线抢占市场,传统移动APP开发的 ...

  7. 【Mysql+shell】查询结果导出到文件,文件数据导入到数据库

    Shell: 执行Mysql查询,并将查询结果导出到文件 直接使用Mysql执行查询 mysql> use xxx_dbName; mysql> select * from log_06 ...

  8. SVM的sklearn实现

    转载:豆-Metcalf 1)SVM-LinearSVC.ipynb-线性分类SVM,iris数据集分类,正确率100% """ 功能:实现线性分类支持向量机 说明:可以 ...

  9. php的语句

    1.php流程语句 1.php代码执行从上到下 2.条件语句 if else 和 switch 案例: $name=56; if($name>56) echo "hello world ...

  10. APP中https证书有效性验证引发安全问题(例Fiddler可抓https包)

    原文: https://blog.csdn.net/woddle/article/details/71175140 在实际项目代码审计中发现,目前很多手机银行虽然使用了https通信方式,但是只是简单 ...