【c++】C++ list erase 错误
来源:互联网 发布:食品批发进销存软件 编辑:程序博客网 时间:2024/05/20 06:29
使用erase时,发生错误。调试后发现是因为,如果在list最后,是需要删除的元素,那么会发生错误,因为erase后,list长度减1,此时的指针已经指向了list的end的下一位指针,即无效的指针,所以会发生错误。在解决的时候,加入判断,是否是最后一个元素,如果是则,指针先减1,然后再用pop_back删除最后一个元素。
如:
for (iter=feature_point->oImagePointDescriptorList.begin();iter!=feature_point->oImagePointDescriptorList.end();iter++){ s=cvGet2D(frame,iter->oPoint.nY,iter->oPoint.nX);//获得该点像素值 注意坐标顺序为(y,x)。 if (s.val[0]>200||s.val[1]>200||s.val[2]>200) //判断像素值大于200 { if (iter==--feature_point->oImagePointDescriptorList.end())//判断是否为list的最后一个元素 { --iter; feature_point->oImagePointDescriptorList.pop_back();//删除最后一个元素 } else iter=feature_point->oImagePointDescriptorList.erase(iter);//删除指定位置的元素,指针加1 } else continue;}
0 0
- 【c++】C++ list erase 错误
- C++primer学习-list中的erase函数
- C++STL:erase
- c++string的erase方法
- 【C++】vector容器和list容器中使用单个迭代器的erase函数学习笔记
- C++/STL用erase删除元素(vector,deque),(list,set,map)
- list-erase
- C++primer学习-vector中的erase函数
- hackerrank>Dashboard>C++>STL>Vector-Erase
- [STL]list的erase正确与错误用法
- List-C
- C list
- 【c/c++】list
- list erase issue
- list的erase
- C/C++--常见错误
- STL: reverse_iterator / iterator 关系以及 erase 相关(C++)
- CodeForces 705C 我被set.erase(*it)套路了
- java中Cookie的使用
- UIPageControl自定义
- C++时间与字符串转换
- Flex编译器错误
- 安卓模拟器DPAD问题,模拟器没有出现键盘,上下左右键也用不了“DPAD not enabled in avd ”
- 【c++】C++ list erase 错误
- 固定SessionID漏洞
- 动态库加载出错,cannot restore segment prot after reloc: Permission denied以及为何编译动态库需要-fPIC
- SetTimer函数和GetTickCount()函数的用法
- Postgres 数据库转移到 MSSQL 2012 手记
- NC57单据开发的一般步骤
- C# SQL和Access连接数据库Helper类,很爽很强大!!!
- hibernate 优化
- 自动编号