C++ STL移除容器元素的方法总结
来源:互联网 发布:linux ffmpeg x264 编辑:程序博客网 时间:2024/05/06 21:49
删除后的元素对应的iterator 就失效了,不可再用,否则导致不可知行为,需要注意!
我总结了3种删除方法: 推荐方法1
1、使用iterator++
map< int, int >::iterator itMap = mapInt.begin();
for ( ; itMap != mapInt.end(); ) {
if ( itMap->second == 2 ) {
// itMap++将使itMap指向下一个元素,但返回原迭代器的副本,所以
// erase()被调用时,itMap不指向将要被删除的元素
mapInt.erase( itMap++ );
}
else {
++itMap;
}
}
2、使用返回值
for(ITER iter=mapInt.begin();iter!=mapInt.end();)
{
if ( itMap->second == 2 ) {
iter=mapTest.erase(iter);
}else {
++iter;
}
}
3、使用tmp变量记录
for(ITER iter=mapInt.begin();iter!=mapInt.end();)
{
if ( itMap->second == 2 ) {
ITER iterTmp = iter+1;
mapTest.erase(iter);
iter = iterTmp;
}else {
++iter;
}
}
0 0
- C++ STL移除容器元素的方法总结
- 总结STL中容器中元素的删除方法
- C++STL:移除map中的元素
- STL-list容器的unique函数移除重复元素的策略
- 【C++】STL容器的总结
- 删除STL容器元素的方法
- c++ STL的各种容器方法总结
- 移除元素方法
- 移除容器中与某值相等的元素
- STL:循环删除容器中元素的方法和陷阱
- STL:循环删除容器中元素的方法和陷阱
- STL:循环删除容器中元素的方法和陷阱
- STL序列式指针容器中删除元素的方法
- Effective STL: 不同容器删除元素的方法
- Effective STL--不同容器删除特定元素的方法
- C++ STL 顺序容器访问最后一个元素的方法
- C++ STL容器遍历删除元素的方法
- 选择删除STL标准容器元素的方法
- PHP技能树---大神的进阶之路
- memcached搭建缓存系统
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- 第17课:给div命名,使逻辑更加清晰
- 移动端calendar日历开发+swiper(支持左右点击,滑屏滚动)
- C++ STL移除容器元素的方法总结
- you need to use a theme.appcompat theme (or descendant) with this activity 解决办法
- [BZOJ1087][SCOI2005][状压DP]互不侵犯King
- HIVE 语句执行 中报错:Java heap space
- 手机浏览器调起微信支付
- snapshotViewAfterScreenUpdates截屏
- eclipse配置mybatis 的xml提示
- 设置Linux ARP表参数
- centos6.5安装python2.7.12