C++ Map详解

来源:互联网 发布:阿里云软件 编辑:程序博客网 时间:2024/05/16 04:49

转自:http://kettleking.iteye.com/blog/1483854

• map定义类型 
/* 
     1. map对象的元素是键值对,也就是每个元素包含两个部分:键以及由键关联的值。 
     键的类型必须是可以比较的,但键类型是自定义类型时,必须重写比较函数: 
     inline bool compare(const keytype &key,const keytype &key) 
     2. map对象的键是不可修改的,值可以;用map迭代器进行解引用将产生pair类型的对象, 
     实际上,迭代器是一个指向pair的引用 
     */ 

C++代码  收藏代码
  1. map<string ,int > test1;  
  2.   
  3. test1["hello"] = 1;  
  4. test1["world"] = 2;  
  5.   
  6. map<string, int>::iterator it1 = test1.begin();  
  7. map<string, int>::iterator it2 = test1.end();  

    /* 
     创建test3,test3中存储迭代器it1,到it3之间的所有元素副本。 
     */ 
C++代码  收藏代码
  1. map<string, int>test3(it1,it2);  
  2. map<string, int>::iterator it3 = test3.begin();  
  3. while (it3 != test3.end())   
  4. {  
  5.     /*map对象的值域是可以修改的*/  
  6.     it3->second = 3;  
  7.     cout << it3->first << ", " << it3->second << endl;  
  8.     it3++;  
  9. }  

• map添加元素 
/** 
     * 1. 使用下标访问map对象 
     *  首先,将键“feigo”及一个默认初始化的值作为一个新键值对插入到test4中。 
     *  然后,读取新插入的元素,并将它的值赋值为4; 
     *  若test4中已经存在该元素,则将该元素值赋值为4. 
     */ 
C++代码  收藏代码
  1. map<string , int > test4;  
  2. test4["feigo"] = 4;   

/** 
     * 2. map::insert的使用 
     */ 
    /** 
     *  • m.insert(e) e是一个m上的value_type类型的值。如果e.first不在m中,则插入e,否则,则保持m不变。 
     *    该函数返回一个pair类型对象,包含指向键为e.first的元素的map迭代器,以及一个bool类型的对象,表示 是否插入该元素 
     */ 
C++代码  收藏代码
  1. map<string , int > test5;  
  2. test5.insert(make_pair("Anna", 5)); // 也可以test5.insert(map<string, int>::value_type("Anna", 5));  
  3. pair< map<string , int >::iterator, bool> resul =  test5.insert(make_pair("Anna", 5));  
  4. map<string , int >::iterator it = resul.first;  
  5. bool isInsert = resul.second;  

    /** 
     *  • m.insert(beg,end) beg,end是某个map对象的迭代器;对于从beg,到end范围内的所有元素,如果它的键在m中不存在,则 
     *    插入到m中。 
     */ 
C++代码  收藏代码
  1. test5.insert(it1,it2);  
  2. map<string, int>::iterator it5 = test5.begin();  
  3. while (it5 != test5.end())   
  4. {  
  5.     cout << it5->first << ", " << it5->second << endl;  
  6.     it5++;  
  7. }   

/** 
     *  • m.insert(iter, e) e是一个中m上的value_type类型值。如果键(e.first)不在m中,则创建新元素,并以迭代器iter为起点 
     *    搜索新元素存储的位置。返回一个迭代器,指向m中具有给定键的元素。 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it6 = test5.begin();  
  2. map<string,int>::iterator it7 = test5.insert(it6,make_pair("Anna", 5));  
  3. cout << "anna : " << it7->first << ", " << it7->second << endl;  

/* 
     * 3. 查找并读取map中的元素 
     */ 
    
    /* 
     * •  m.count(k) 返回m中k的出现次数,对于map对象,其返回值只能是0或者1. 
     *    如果返回值非0,则可以使用下标操作符来获取该键所关联的值,而不用担心这样做会中map中插入新元素           
     */ 
C++代码  收藏代码
  1. if(test5.count("Anna"))  
  2. {  
  3.     cout << test5["Anna"] << endl;  
  4. }  

    /* 
     * •  m.find(k) 如果m容器中存在按k键索引的元素,则返回指向该元素的迭代器。如果不存在,则返回超出末端迭代器。 
     *    和count相比,find只查找了一次,而count查找了两次 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it8 = test5.find("Anna");  
  2. if(it8 != test5.end())  
  3. {  
  4.     cout << it8->second << endl;  
  5. }  

/* 
     * 4.  从map对象中删除元素 
     */ 
    
    /* 
     * •  m.erase(k) 删除m中键为k的元素。返回size_type类型的值,表示删除的元素个数。非0即1,不解释。 
     */ 
C++代码  收藏代码
  1. if (test5.erase("Anna"))   
  2. {  
  3.     it8 = test5.begin();  
  4.     while (it8 != test5.end())   
  5.     {  
  6.         cout << it8->first << ", " << it8->second << endl;  
  7.         it8++;  
  8.     }  
  9. }  
  
    /* 
     * •  m.erase(p) 删除m中迭代器所指向的元素。p必须指向m中确实存在的元素,而且不能等于m.end()。否则程序中运行时出错。 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it9 = test5.find("Anna");  
  2. test5.erase(it9);  //运行至此挂了  
  3. it9 = test5.begin();  
  4. while (it9 != test5.end())  
  5. {  
  6.     cout << it9->first << ", " << it9->second << endl;  
  7.     it9++;   
  8. }  

    /* 
     * •  m.erase(b,e) 删除m中迭代器b,e之间的元素。b,e必须标示m中的一段有效范围。不然:运行时挂! 
     */ 
C++代码  收藏代码
  1. map<string,int>::iterator it10 = test5.begin();  
  2. map<string,int>::iterator it11 = test5.end();  
  3. test5.erase(it10,it11);  
  4. cout << endl;  
  5. while (it10 != test5.end())  
  6. {  
  7.     cout << it10->first << ", " << it10->second << endl;  
  8.     it10++;   
  9. }