免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4909 | 回复: 2
打印 上一主题 下一主题

[C++] map遍历并删除项,为什么没有crash呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-24 23:33 |只看该作者 |倒序浏览
形如下面的代码:

map<int,TestObject *>::iterator it = obj_map.begin();   
for(; it!=obj_map.end();it++ ){            
          obj_map.erase(a);         
}
网上很多人都说会崩溃,直觉上也应该崩。可是下面的函数运行的时候并没有崩溃:

class TestObject{   
private:
    int a;                           
public:   
    TestObject(int a){        
        this->a = a;        
        cout<<"TestObj init.one parameter:a="<<a<<endl;
    }                        
    ~TestObject(){
        cout<<"TestObj desctruction"<<endl;
    };   
    int getA(){
        return a;   
    }
};   
void test_map()
{
    cout<<__func__<<endl;
    map<int,TestObject *> obj_map;
    obj_map[1] = new TestObject(1);
    obj_map[2] = new TestObject(2);               
    obj_map[3] = new TestObject(3);
    obj_map[4] = new TestObject(4);
    obj_map[5] = new TestObject(5);
    obj_map[6] = new TestObject(6);
            
    TestObject *ObjArray[4] = {0,0,0,0};   
            
    cout<<endl<<"obj_map size = "<<obj_map.size()<<endl;
   
    int i = 0;        
    int a = -1;        
    map<int,TestObject *>::iterator it = obj_map.begin();   
    for(; it!=obj_map.end();it++ ){      
        a = it->first;   
        if (0==(a%2)){
                cout<<"erase.a = "<<a<<endl;
                ObjArray[i++] = it->second;               
                obj_map.erase(a);   
        }        
        a = -1;
    }        
    cout<<endl<<"obj_map 大小是 "<<obj_map.size()<<endl;
    cout<<"删除的个数 "<<i<<endl<<endl;

    cout<<"obj_map中对象是:"<<endl;   
    it = obj_map.begin();
    for(; it!=obj_map.end();it++ ){
        cout<<it->first<<endl;   
    }            
   
    cout<<endl<<"删除的对象仍然存在,他们是:"<<endl;;
    for(i=0; i<4; i++){
        if (ObjArray){                 
                 cout<<"a="<<ObjArray->getA()<<endl;
        }               
    }           
}

test_map在dev-cpp下输出的结果是:

test_map
TestObj init.one parameter:a=1
TestObj init.one parameter:a=2
TestObj init.one parameter:a=3
TestObj init.one parameter:a=4
TestObj init.one parameter:a=5
TestObj init.one parameter:a=6

obj_map size = 6
erase.a = 2
erase.a = 4
erase.a = 6

obj_map 大小是 3
删除的个数 3

obj_map中对象是:
1
3
5

删除的对象仍然存在,他们是:
a=2
a=4
a=6
请按任意键继续. . .
为什么没有崩溃呢?
realfun 该用户已被删除
2 [报告]
发表于 2008-07-25 00:12 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2008-07-25 09:23 |只看该作者
其实我就是想在一次遍历之中删除map中的某些项。

改成这样如何?

#include <iostream>
#include <map>
#include "TestMap.h"

using namespace std;

class TestObject{   
private:
    int a;                           
public:   
    TestObject(int a){        
        this->a = a;        
        cout<<"TestObj init.one parameter:a="<<a<<endl;
    }                        
    ~TestObject(){
        cout<<"TestObj desctruction"<<endl;
    };   
    int getA(){
        return a;   
    }
};   
void test_map1()
{
    cout<<__func__<<endl;
    map<int,TestObject *> obj_map;
    obj_map[1] = new TestObject(1);
    obj_map[2] = new TestObject(2);               
    obj_map[3] = new TestObject(3);
    obj_map[4] = new TestObject(4);
    obj_map[5] = new TestObject(5);
    obj_map[6] = new TestObject(6);
            
    TestObject *ObjArray[4] = {0,0,0,0};   
            
    cout<<endl<<"obj_map size = "<<obj_map.size()<<endl;
   
    int i = 0;        
    int a = -1;        
    map<int,TestObject *>::iterator it = obj_map.begin();   
    for(; it!=obj_map.end();it++ ){      
        a = it->first;   
        if (0==(a%2)){
                cout<<"erase.a = "<<a<<endl;
                ObjArray[i++] = it->second;               
                obj_map.erase(a);   
        }        
        a = -1;
    }        
    cout<<endl<<"obj_map 大小是 "<<obj_map.size()<<endl;
    cout<<"删除的个数 "<<i<<endl<<endl;

    cout<<"obj_map中对象是:"<<endl;   
    it = obj_map.begin();
    for(; it!=obj_map.end();it++ ){
        cout<<it->first<<endl;   
    }            
   
    cout<<endl<<"删除的对象仍然存在,他们是:"<<endl;;
    for(i=0; i<4; i++){
        if (ObjArray){                 
                 cout<<"a="<<ObjArray->getA()<<endl;
        }               
    }           
}

void test_map()
{
    cout<<__func__<<endl;
    map<int,TestObject *> obj_map;
    obj_map[1] = new TestObject(1);
    obj_map[2] = new TestObject(2);               
    obj_map[3] = new TestObject(3);
    obj_map[4] = new TestObject(4);
    obj_map[5] = new TestObject(5);
    obj_map[6] = new TestObject(6);
            
    TestObject *ObjArray[4] = {0,0,0,0};   
            
    cout<<endl<<"obj_map size = "<<obj_map.size()<<endl;
    //遍历删除
    int i = 0;        
    int a = -1;        
    TestObject *p = NULL;
    map<int,TestObject *>::iterator it = obj_map.begin();     
   
    while (it!=obj_map.end()){      
        a = it->first;   
        p = it->second;  
        it++;
        if (0==(a%2)){
                cout<<"erase.a = "<<a<<endl;
                ObjArray[i++] = p;               
                obj_map.erase(a);   
        }        
        a = -1;
    }        //遍历删除
    cout<<endl<<"obj_map 大小是 "<<obj_map.size()<<endl;
    cout<<"删除的个数 "<<i<<endl<<endl;

    cout<<"obj_map中对象是:"<<endl;   
    it = obj_map.begin();
    for(; it!=obj_map.end();it++ ){
        cout<<it->first<<endl;   
    }            
   
    cout<<endl<<"删除的对象仍然存在,他们是:"<<endl;;
    for(i=0; i<4; i++){
        if (ObjArray){                 
                 cout<<"a="<<ObjArray->getA()<<endl;
        }               
    }           
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP