现在的位置: 首页 > 综合 > 正文

STL学习之map容器

2018年04月02日 ⁄ 综合 ⁄ 共 5485字 ⁄ 字号 评论关闭

From:

http://blog.csdn.net/richerg85/article/details/7686481

 

map::insert

  1. pair<iterator,bool> insert(const value_type& x);  
  2.                      iterator insert(iterator position, const value_type& x);  
  3. template<class InputIterator>  
  4.                           void insert(InputIterator first, InputIterator last);  

插入元素

map容器可以插入单个新元素(如果使用参数x)或者元素序列(如果使用输入迭代器)。

随着插入元素数量增加,容器的size也随着增加。

由于map容器不允许键值重复,所以插入操作需要确保要插入的key值在map容器中没有。如果有的话,新的元素是不允许插入的,并且此键值的映射值是不会发生改变的。

如果想允许插入相同的元素,可以参考multimap。

本质上,map容器保持所有元素按照结构体指定的比较对象排序。

map容器允许直接的访问通过map::operator[]操作(对[]操作符进行了重载),即可以通过object[key]=value;这样操作。

参数

x

    初始化插入元素的值。

position

 确定插入操作时第一个相比较的元素。该参数并不是强制插入元素的位置。

first,last

指定元素区间的迭代器。拷贝[first,last)区间的元素,并且插入到map容器中。


返回值

第一个版本的insert返回一个pair类型,pair类型的成员pair:first设置一个迭代器指针,这个迭代器指针可能指向新插入的元素或者已经在map容器中存在的有着相同key值的元素;成员pair:second元素,如果新的元素插入,返回为true,或者当已经存在相同的key,返回false。

第二个版本返回一个指向新插入的元素或者已经在map容器中存在的有着相同key值的元素的迭代器指针。

迭代器是一个成员类型,被定义成双向迭代器类型。

举例1:

  1. #include <string>
      
  2. #include <iostream>
      
  3. #include <map>
      
  4. #include <utility>
      
  5.   
  6. using namespace std;  
  7.   
  8. int main(void)  
  9. {  
  10.         map<string, int> Employees;  
  11.   
  12.         Employees["Mike C."] = 12306;  
  13.         Employees["Charlie M."] = 95555;  
  14.         //使用pair
      
  15.         Employees.insert(std::pair<string, int>("David D.", 1984));  
  16.         Employees.insert(map<string, int>::value_type("John A.", 7582));  
  17.         Employees.insert(make_pair("Peter Q.", 5328));  
  18.   
  19.         cout << "Map size: " << Employees.size() <<  endl;  
  20.   
  21.         for (map<string, int>::iterator ii= Employees.begin(); ii != Employees.end(); ++ii)  
  22.         {  
  23.                     cout << (*ii).first << ":" << (*ii).second << endl;  
  24.         }  
  25.         return 0;  
  26. }                                                  

执行结果:

  1. liujl@liujl-Rev-1-0:~/mycode/STL$ ./mapEx2  
  2. Map size: 5  
  3. Charlie M.:95555  
  4. David D.:1984  
  5. John A.:7582  
  6. Mike C.:12306  
  7. Peter Q.:5328  

实例2:

  1. #include <iostream>
      
  2. #include <map>
      
  3.   
  4. using namespace std;  
  5.   
  6. int main(void)  
  7. {  
  8.     map<charint> mymap;  
  9.     map<charint>::iterator it;  
  10.     pair<map<charint>::iterator, bool> ret;  
  11.   
  12.     //first insert function version
      
  13.     mymap.insert( pair<charint>('a', 100) );  
  14.     mymap.insert( make_pair('d', 200) );  
  15.     mymap.insert( map<charint>::value_type('z', 500));  
  16.       
  17.     //check return value
      
  18.     ret = mymap.insert( pair<charint>('z', 800));  
  19.     if (ret.second == false)  
  20.     {  
  21.         cout << " element 'z' already existed!";  
  22.         cout << " with a value of " << ret.first->second << endl;  
  23.     }  
  24.   
  25.     //second insert function version
      
  26.     it = mymap.begin();  
  27.     mymap.insert(it, pair<charint>('b', 300));  
  28.     mymap.insert(it, pair<charint>('c', 400));  
  29.       
  30.     //third insert function version
      
  31.     map<charint> othermap;  
  32.     othermap.insert(mymap.begin(), mymap.end());  
  33.     //othermap.insert(mymap.begin(), mymap.find('c'));
      
  34.       
  35.     //print container contents:
      
  36.   
  37.     cout << "mymap contains: " << endl;  
  38.     for (it = mymap.begin(); it != mymap.end(); it++)  
  39.         cout << (*it).first << "=>" << (*it).second << endl;  
  40.       
  41.     cout << "other contains: " << endl;  
  42.     for (it=othermap.begin(); it != othermap.end(); it++)  
  43.         cout << (*it).first << "=>" << (*it).second << endl;  
  44.     return 0;  
  45. }  

执行结果:

  1. liujl@liujl-Rev-1-0:~/mycode/STL$ vim map_insert.cpp  
  2. liujl@liujl-Rev-1-0:~/mycode/STL$ g++ map_insert.cpp  -o map_insert  
  3. liujl@liujl-Rev-1-0:~/mycode/STL$ ./map_insert  
  4.  element 'z' already existed! with a value of 500  
  5. mymap contains:   
  6. a=>100  
  7. b=>300  
  8. c=>400  
  9. d=>200  
  10. z=>500  
  11. other contains:   
  12. a=>100  
  13. b=>300  
  14. c=>400  
  15. d=>200  
  16. z=>500  

抱歉!评论已关闭.