ERROR: iterator not incrementable || iterator not decrementable
这个错误提示:迭代器不可以增加
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的更多相关文章
- 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 ...
- VC++ 迭代器 iterator, const_iterator, const iterator
迭代器 iterator, const_iterator, const iterator 迭代器iterator的作用类似于指针. (1)iterator只有针对制定<类型>的容器才有效. ...
- [转]vector iterator not incrementable 的问题
转自:http://blog.csdn.net/kuaile123/article/details/11105115 vector::erase误使用问题: 暂时使用经验: 不能在循环中使用,否则会报 ...
- map set iterator not incrementable 解决办法
例子: #include <iostream> #include <map> using namespace std; int main() { map<int, int ...
- java.lang.NoSuchMethodError: org.json.JSONArray.iterator()Ljava/util/Iterator 阿里云短信
请尝试使用 <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk ...
- javascript设计模式-迭代器模式(Iterator)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- c++迭代器(iterator)详解
1. 迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型.(1) 每种容器类型都定义了自己的迭代器类型,如vector:vector<int>::iterator iter ...
- Peeking Iterator 解答
Question Given an Iterator class interface with methods: next() and hasNext(), design and implement ...
- 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录
1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误 解决办法很简单,不能让list为空 2 刚进入该界面的时候list是有数据的,当点击提 ...
随机推荐
- Vivado HLS初识---阅读《vivado design suite tutorial-high-level synthesis》
Vivado HLS初识---阅读<vivado design suite tutorial-high-level synthesis> 1.启动 2.创建工程 3.添加源文件 4.添加测 ...
- win764位系统上让32位程序能申请到4GB内存方法
win764位系统上让32位程序能申请到4GB内存方法. 2016年09月18日 18:36:26 阅读数:1550 最近测试一个32位程序总是在1.2G左右内存时崩溃,怀疑是内存申请失败,本身32位 ...
- jenkins构建触发器详解-不登录触发远程构建
利用jenkins的远程构建功能,我们可以使用任何脚本,甚至定制一个Web页来控制Job的执行,但是远程构建你如果直接使用的话,老是需要登录才能执行,如何避免登录?稍微折腾了一下,调通了. 1.首先去 ...
- sql Find_IN_SET 用法
字段以 1,2,3,4 格式存储的SELECT * from test where FIND_IN_SET('15',btype) GROUP_CONCAT + group_by
- jar打包混淆上传全自动日志
第一步: Java的pom.xml文件中要加入导出lib的插件.如下: <build> <plugins> <plugin> <groupId>org. ...
- 面试总结之人工智能AI(Artificial Intelligence)/ 机器学习(Machine Learning)
刚面完 AI 岗,这几点分享给你!- AI科技大本营 https://mp.weixin.qq.com/s/05G5HKSkZwhwnmskijToLQ 1.训练决策树时的参数是什么? 2.在决策树的 ...
- [转]SuperSocket
public class SocketServer : AppServer<AppSession> { public SocketServer() : base(new DefaultRe ...
- HTML和SEO基础知识:H标签全透视
原文地址:http://www.chinaz.com/web/2010/0921/134391.shtml 什么是H标签? h1,h2,h3,h4,h5,h6,作为标题标签,并且依据重要性递减.我认为 ...
- Spring AOP 之编译期织入、装载期织入、运行时织入(转)
https://blog.csdn.net/wenbingoon/article/details/22888619 一 前言 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP ...
- RabbitMQ入门教程(十):队列声明queueDeclare(转载)
原文转载至:https://blog.csdn.net/vbirdbest/article/details/78670550 简介本节主要讨论队列声明的各个参数 queueDeclare(String ...