天下

记录修行的印记

C++ list/map 迭代器中删除元素

C++ list 迭代器中删除元素

#include <iostream>
#include 
<list>
#include 
<map>
#include 
<afxwin.h>
using namespace std;
int main ()
{
    list
<int> listInts;
    listInts.push_back(
1);
    listInts.push_back(
2);
    listInts.push_back(
3);
    listInts.push_back(
4);
    listInts.push_back(
5);
    list
<int>::iterator iter;
    
for(iter = listInts.begin();iter !=listInts.end();){
        cout 
<< *iter << endl;
        
if ( *iter == 3){
            iter 
= listInts.erase(iter);
        }
        
else{
            iter
++;
        }
    }
    
for(iter = listInts.begin();iter !=listInts.end();iter++){
        cout 
<< *iter << endl;
    }
    
return 0;
}

std::map<int,int>::iterator iter;
for (iter=maps.begin();iter!=maps.end();)
{
    
if (expression) {
        maps.erase(iter
++);
    }
    
else {
        iter
++;
    }
}

posted on 2010-11-18 17:26 天下 阅读(3889) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年11月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论