Map 和 multimap

来源:互联网 发布:淘宝平面模特兼职 编辑:程序博客网 时间:2024/04/29 15:48

 

一、

Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!
1. map构造函数;
map<string , int >mapstring; map<int ,string >mapint;
map<sring, char>mapstring; map< char ,string>mapchar;
map<char ,int>mapchar; map<int ,char >mapint;

如在打枚举中打印 “指定值对应的字符串”时,可是采用map<int, string>的STL实现。

以前我们是这样打印信息出来的:

[cpp] view plain copy
  1. static inline const char *   
  2. VNET_TYPE_STRING(vnet_type_t type)  
  3. {  
  4.     static VALUE_STRING_STRUCT g_type_string[] =  
  5.     {  
  6.         { VNET_TYPE_UNKOWN, "unkown1" },  
  7.         { VNET_TYPE_SOCKET, "socket" },  
  8.         { VNET_TYPE_RDP,    "rdp" },  
  9.         { VNET_TYPE_PCOIP,  "pcoip" },  
  10.         { VNET_TYPE_ICA, "ica" },  
  11.         { VNET_TYPE_XRED, "xred" },  
  12.         { 0, NULL },  
  13.     };  
  14.     return GetValueString(g_type_string, (ULONG)type);  
  15. }  
[cpp] view plain copy
  1. static inline const TCHAR *  
  2. GetValueString(VALUE_STRING_STRUCT *vsarray, ULONG value)  
  3. {  
  4.     VALUE_STRING_STRUCT *tmp = vsarray;  
  5.   
  6.     while ( tmp->string != NULL )  
  7.     {  
  8.         if ( tmp->value == value )  
  9.         {  
  10.             return tmp->string;  
  11.         }  
  12.         tmp++;  
  13.     }  
  14.     return _T("unkown");  
  15. }  



 

2. map添加数据;
map<int ,string> maplive; 
1.maplive.insert(pair<int,string>(102,"aclive"));
2.maplive.insert(map<int,string>::value_type(321,"hai"));
3, maplive[112]="April";//map中最简单最常用的插入添加!

3,map中元素的查找:

find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。

map<int ,string >::iterator l_it;; 
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else cout<<"wo find 112"<<endl;


4,map中元素的删除:
如果删除112;
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else maplive.erase(l_it); //delete 112;

 


5,map中 swap的用法
Map中的swap不是一个容器中的元素交换,而是两个容器交换

For example:

[cpp] view plain copy
  1. #include <map>  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6.   
  7. int main( )  
  8. {  
  9.     map <intint> m1, m2, m3;  
  10.     map <intint>::iterator m1_Iter;  
  11.   
  12.     m1.insert ( pair <intint> ( 1, 10 ) );  
  13.     m1.insert ( pair <intint> ( 2, 20 ) );  
  14.     m1.insert ( pair <intint> ( 3, 30 ) );  
  15.     m2.insert ( pair <intint> ( 10, 100 ) );  
  16.     m2.insert ( pair <intint> ( 20, 200 ) );  
  17.     m3.insert ( pair <intint> ( 30, 300 ) );  
  18.   
  19.     cout << "The original map m1 is:";  
  20.     for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )  
  21.         cout << " " << m1_Iter->second;  
  22.     cout << "." << endl;  
  23.   
  24.     // This is the member function version of swap  
  25.     //m2 is said to be the argument map; m1 the target map  
  26.     m1.swap( m2 );  
  27.   
  28.     cout << "After swapping with m2, map m1 is:";  
  29.     for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )  
  30.         cout << " " << m1_Iter -> second;  
  31.     cout << "." << endl;  
  32.     cout << "After swapping with m2, map m2 is:";  
  33.     for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )  
  34.         cout << " " << m1_Iter -> second;  
  35.     cout << "." << endl;  
  36.     // This is the specialized template version of swap  
  37.     swap( m1, m3 );  
  38.   
  39.     cout << "After swapping with m3, map m1 is:";  
  40.     for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )  
  41.         cout << " " << m1_Iter -> second;  
  42.     cout << "." << endl;  
  43.   
  44.     system("pause");  
  45. }  


运行结果是:

6.map的sort问题:
Map中的元素是自动按key升序排序,所以不能对map用sort函数
For example:

[cpp] view plain copy
  1. #include <map>  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6.   
  7. int main( )  
  8. {  
  9. map <intint> m1;  
  10. map <intint>::iterator m1_Iter;  
  11.   
  12. m1.insert ( pair <intint> ( 1, 20 ) );  
  13. m1.insert ( pair <intint> ( 4, 40 ) );  
  14. m1.insert ( pair <intint> ( 3, 60 ) );  
  15. m1.insert ( pair <intint> ( 2, 50 ) );  
  16. m1.insert ( pair <intint> ( 6, 40 ) );  
  17. m1.insert ( pair <intint> ( 7, 30 ) );  
  18.   
  19. cout << "The original map m1 is:"<<endl;  
  20. for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )  
  21. cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;  
  22.   
  23. }  


7, map的基本操作函数:
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数


二、C++中map用法

[cpp] view plain copy
  1. /************************************************************************ 
  2. * 
  3. * Map的特点: 1、存储Key-value对 
  4. * 2、支持快速查找,查找的复杂度基本是Log(N) 
  5. * 3、快速插入,快速删除,快速修改记 
  6. * 
  7. /************************************************************************/  
  8. #include <iostream>  
  9. #include <string>  
  10. #include <map>   
  11. using namespace std;  
  12.   
  13.   
  14. int main()  
  15. {  
  16.     map<const char*,int> m;  
  17.     m["a"]=1;  
  18.     m["b"]=6;  
  19.     m["c"]=9;  
  20.     map<const char*,int>::iterator it;  
  21.     it=m.begin();  
  22.     const char* c =it->first;  
  23.     cout<<"first element is :"<<c<<endl;  
  24.     int i = m["c"];  
  25.     while(it!=m.end()){  
  26.         cout << it->first<<";"<<it->second<<endl;  
  27.         ++it;  
  28.     }  
  29.     cout <<"m[\"c\"]="<<i<<endl;  
  30.     cout <<"sizeof m:"<<m.size()<<endl;  
  31.     cout <<"erase m[\"c\"](1:succ 0:failed):"<<m.erase("c")<<endl;  
  32.     cout <<"erase m[\"c\"]:"<<m.erase("c")<<endl;  
  33.     cout <<"sizeof m:"<<m.size()<<endl;  
  34.     cout<<"m[c]="<<m["c"]<<endl;  
  35.     cout<<"sizeof m :"<<m.size()<<endl;  
  36.   
  37.     system("pause");  
  38.     return 0;  
  39.   
  40. }  


 

请注意上面打感叹号的地方,map的大小因此调用了“cout<<"m[c]="<<m["c"]<<endl;”而增加了1个。这点真的另人很奇怪。!!!!

 

 

三、multimap 的使用方法

 

[cpp] view plain copy
  1. /* 
  2.  * 
  3.  ******************************************** 
  4.  *  multimap多重映照容器的基础说明: 
  5.  ******************************************** 
  6.  * 
  7.  * multimap多重映照容器:容器的数据结构采用红黑树进行管理 
  8.  * multimap的所有元素都是pair:第一元素为键值(key),不能修改;第二元素为实值(value),可被修改 
  9.  * 
  10.  * multimap特性以及用法与map完全相同,唯一的差别在于: 
  11.  * 允许重复键值的元素插入容器(使用了RB-Tree的insert_equal函数)  
  12.  * 因此: 
  13.  * 键值key与元素value的映照关系是多对多的关系 
  14.  * 没有定义[]操作运算  
  15.  *  
  16.  * Sorted Associative Container  Pair Associative Container   Unique Associative Container 
  17.  * 
  18.  * 使用multimap必须使用宏语句#include <map>           
  19.  * 
  20.  ************************************************************************************** 
  21.  * 
  22.  * 创建multimap对象: 
  23.  * 1.multimap<char,int,greater<char> > a;    //元素键值类型为char,映照数据类型为int,键值的比较函数对象为greater<char> 
  24.  * 2.multimap(const key_compare& comp)     //指定一个比较函数对象comp来创建map对象 
  25.  *  3.multimap(const multisetr&);      //multimap<int,char*> b(a); //此时使用默认的键值比较函数less<int> 
  26.  * 4.multimap(first,last);          
  27.  * 5.multimap(first,last,const key_compare& comp);   
  28.  * 
  29.  * //Example: 
  30.  * pair<const int ,char> p1(1,'a'); 
  31.  * pair<const int ,char> p2(2,'b'); 
  32.  * pair<const int ,char> p3(3,'c'); 
  33.  * pair<const int ,char> p4(4,'d'); 
  34.  * pair<const int ,char> pairArray[]={p1,p2,p3,p4}; 
  35.  * multimap<const int,char> m4(pairArray,pairArray+5); 
  36.  * multimap<const int,char> m3(m4); 
  37.  * multimap<const int,char,greater<const int> > m5(pairArray,pairArray+5,greater<const int>()); 
  38.  * 
  39.  ************************************************************************************** 
  40.  * 
  41.  * 元素的插入 
  42.  * //typedef pair<const key,T> value_type; 
  43.  * pair<iterator,bool> insert(const value_type& v);     
  44.  * iterator insert(iterator pos,const value_type& v); 
  45.  * void insert(first,last); 
  46.  * 
  47.  ************************************************************************************** 
  48.  * 
  49.  * 元素的删除 
  50.  * void erase(iterator pos); 
  51.  * size_type erase(const key_type& k);     //删除等于键值k的元素 
  52.  * void erase(first,last);        //删除[first,last)区间的元素 
  53.  * void clear(); 
  54.  * 
  55.  ************************************************************************************** 
  56.  * 
  57.  * 访问与搜索 
  58.  * 
  59.  * iterator begin();iterator end();     //企图通过迭代器改变元素是不被允许的 
  60.  * reverse_iterator rbegin();reverse_iterator rend(); 
  61.  * 
  62.  * iterator find(const key_type& k) const; 
  63.  * pair<iterator,iterator> equal_range(const key_type& k) const;//返回的pair对象, 
  64.  *                //first为lower_bound(k);大于等于k的第一个元素位置 
  65.  *                //second为upper_bound();大于k的第一个元素位置 
  66.  * 
  67.  * 其它常用函数 
  68.  * bool empty() const; 
  69.  * size_type size() const; 
  70.  * size_type count(const key_type& k) const;   //返回键值等于k的元素个数 
  71.  * void swap(); 
  72.  * 
  73.  * iterator lower_bound();iterator upper_bound();pair<iterator,iterator> equal_range();//上界、下届、确定区间 
  74.  * 
  75.  * 
  76.  * 
  77.  ******************************************** 
  78.  **   cumirror ** tongjinooo@163.com **    ** 
  79.  ******************************************** 
  80.  * 
  81.  */  
  82.   
  83. #include <map>  
  84. #include <string>  
  85. #include <iostream>  
  86.   
  87. // 基本操作与set类型,牢记map中所有元素都是pair  
  88. // 对于自定义类,初学者会觉得比较函数如何构造很麻烦,这个可以参照前面的书写示例  
  89. // 但若设置键值为int或char类型,无须构造比较函数  
  90.   
  91. struct student{  
  92.  char* name;  
  93.  int age;  
  94.  char* city;  
  95.  char* phone;  
  96. };  
  97.   
  98. int main()  
  99. {  
  100.      using namespace std;  
  101.   
  102.      student s[]={  
  103.       {"童进",23,"武汉","XXX"},  
  104.       {"老大",23,"武汉","XXX"},  
  105.       {"饺子",23,"武汉","XXX"},  
  106.       {"王老虎",23,"武汉","XXX"},  
  107.       {"周润发",23,"武汉","XXX"},  
  108.       {"周星星",23,"武汉","XXX"}  
  109.      };  
  110.       pair<int,student> p1(4,s[0]);  
  111.       pair<int,student> p2(2,s[1]);  
  112.       pair<int,student> p3(3,s[2]);  
  113.       pair<int,student> p4(4,s[3]);  //键值key与p1相同  
  114.       pair<int,student> p5(5,s[4]);  
  115.       pair<int,student> p6(6,s[5]);  
  116.      multimap<int,student> a;  
  117.      a.insert(p1);  
  118.      a.insert(p2);  
  119.      a.insert(p3);  
  120.      a.insert(p4);  
  121.      a.insert(p5);  
  122.      a.insert(p6);  
  123.      typedef multimap<int,student>::iterator int_multimap;  
  124.      pair<int_multimap,int_multimap> p = a.equal_range(4);  
  125.      int_multimap i = a.find(4);  
  126.      cout<<"班上key值为"<< i->first<<"的学生有:"<<a.count(4)<<"名,"<<"   他们是:"<<endl;  
  127.      for(int_multimap k = p.first; k != p.second; k++)  
  128.      {  
  129.         cout<<k->second.name<<endl;  
  130.      }  
  131.      cout<<"删除重复键值的同学"<<endl;  
  132.      a.erase(i);  
  133.      cout<<"现在班上总人数为:"<<a.size()<<".   人员如下:"<<endl;  
  134.      for(multimap<int,student>::iterator j=a.begin(); j != a.end(); j++)  
  135.      {        
  136.           cout<<"The name: "<<j->second.name<<"      "<<"age: "<<j->second.age<<"   "  
  137.            <<"city: "<<j->second.city<<"      "<<"phone: "<<j->second.phone<<endl;  
  138.      }  
  139.   
  140.      return 0;  
  141. }  

0 0
原创粉丝点击