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是有数据的,当点击提 ...
随机推荐
- python调用hanlp分词包手记
python调用hanlp分词包手记 Hanlp作为一款重要的分词工具,本月初的时候看到大快搜索发布了hanlp的1.7版本,新增了文本聚类.流水线分词等功能.关于hanlp1.7版本的新功能,后 ...
- Linux patch命令详解
Linux patch命令 Linux patch命令用于修补文件. patch指令让用户利用设置修补文件的方式,修改,更新原始文件.倘若一次仅修改一个文件,可直接在指令列中下达指令依序执行.如果配合 ...
- 文件上传失败 -nginx报错 client intended to send too large body: 1331696 bytes
location / { root /data/fastdfs/data; include gzip.conf; ngx_fastdfs_module; client_max_body_size 10 ...
- 关于java多线程理解到集群分布式和网络设计的浅析
对于JAVA多线程的应用非常广泛,现在的系统没有多线程几乎什么也做不了,很多时候我们在何种场合如何应用多线程成为一种首先需要选择的问题, 另外关于java多线程的知识也是非常的多,本文中先介绍和说明一 ...
- ALGO-18_蓝桥杯_算法训练_单词接龙(搜索)
问题描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...
- 【linux】安装docker
硬件Centos6.9 x86_64 1.查看centos内核,uname -r 目前,CentOS 仅发行版本中的内核支持 Docker. Docker 运行在 CentOS 7 上,要求系统为64 ...
- C++11--时钟和计时器<chrono>
/* 介绍<chrono> -- 一个精确中立的时间和日期库 * 时钟: * * std::chrono::system_clock: 依据系统的当前时间 (不稳定) * std::chr ...
- uoj #49. 【UR #3】铀仓库
http://uoj.ac/problem/49 这题二分答案可以做,同时存在另一个直接二分的解法. 考虑对每个点,二分能向左右延伸的最大半径,由于权值范围较大,不能O(1)查询向一侧走指定距离后到达 ...
- july 大神 要向他学习的东西(已学了)
交换礼物代码 库 permutations 库 product https://www.cnblogs.com/kaibindirver/p/10714375.html https://www.cnb ...
- levenshtein函数
Levenshtein算法已在部分DBMS中实现. (例如:PostgreSQL:http://www.postgresql.org/docs/9.1/Static/fuzzystrmedi.html ...