标签:
看到map这里,都不知道它主要是干嘛的,你有没有这样的疑问。
map的主要作用:提供对T类型的数据进行快速和高效的检索 。C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。 注:map是自动按key值排序的,默认为升序或字典排序
(1)map的定义:
int nFindKey = 2; //要查找的Key //定义一个条目变量(实际是指针) UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey); if(it == enumMap.end()) { cout<<"没找到"<<endl; } else { cout<<"找到了"<<endl; }
1 #include <map> 2 #include <iostream> 3 using namespace std; 4 int main( ) 5 { 6 map <int, int> m1, m2, m3; 7 map <int, int>::iterator m1_Iter; 8 9 m1.insert ( pair <int, int> ( 1, 10 ) ); 10 m1.insert ( pair <int, int> ( 2, 20 ) ); 11 m1.insert ( pair <int, int> ( 3, 30 ) ); 12 13 m2.insert ( pair <int, int> ( 10, 100 ) ); 14 m2.insert ( pair <int, int> ( 20, 200 ) ); 15 16 m3.insert ( pair <int, int> ( 30, 300 ) ); 17 18 cout << "The original map m1 is:"; 19 for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) 20 cout << " " << m1_Iter->second; 21 cout << "." << endl; 22 // This is the member function version of swap 23 //m2 is said to be the argument map; m1 the target map 24 25 m1.swap( m2 ); 26 cout << "After swapping with m2, map m1 is:"; 27 for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) 28 cout << " " << m1_Iter -> second; 29 cout << "." << endl; 30 31 cout << "After swapping with m2, map m2 is:"; 32 for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ ) 33 cout << " " << m1_Iter -> second; 34 cout << "." << endl; 35 36 // This is the specialized template version of swap 37 swap( m1, m3 ); 38 cout << "After swapping with m3, map m1 is:"; 39 for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) 40 cout << " " << m1_Iter -> second; 41 cout << "." << endl; 42 }
下面再说几个例子:
1 #include <iostream> 2 3 #include <map> 4 5 using namespace std; 6 7 int main(void) 8 9 { 10 11 map<char,int,less<char> > map1; 12 13 map<char,int,less<char> >::iterator mapIter; 14 15 //char 是键的类型,int是值的类型 16 17 //下面是初始化,与数组类似 18 19 //也可以用map1.insert(map<char,int,less<char> >::value_type(‘c‘,3)); 20 21 map1[‘c‘]=3; 22 23 map1[‘d‘]=4; 24 25 map1[‘a‘]=1; 26 27 map1[‘b‘]=2; 28 29 for(mapIter=map1.begin();mapIter!=map1.end();++mapIter) 30 31 cout<<" "<<(*mapIter).first<<": "<<(*mapIter).second; 32 33 //first对应定义中的char键,second对应定义中的int值 34 35 //检索对应于d键的值是这样做的: 36 37 map<char,int,less<char> >::const_iterator ptr; 38 39 ptr=map1.find(‘d‘); 40 41 cout<<‘‘\n‘‘<<" "<<(*ptr).first<<" 键对应于值:"<<(*ptr).second; 42 43 cin.get(); 44 45 return 0; 46 47 } 48 49 //从以上例程中,我们可以看到map对象的行为和一般数组的行为类似。Map允许两个或多个值使用比较操作符。
关于multimap:
#include <iostream> #include <map> #include <string> using namespace std; int main(void) { multimap<string,string,less<string> >mulmap; multimap<string,string,less<string> >::iterator p; //初始化多重映射mulmap: typedef multimap<string,string,less<string> >::value_type vt; typedef string s; mulmap.insert(vt(s("Tom "),s("is a student"))); mulmap.insert(vt(s("Tom "),s("is a boy"))); mulmap.insert(vt(s("Tom "),s("is a bad boy of blue!"))); mulmap.insert(vt(s("Jerry "),s("is a student"))); mulmap.insert(vt(s("Jerry "),s("is a beatutiful girl"))); mulmap.insert(vt(s("DJ "),s("is a student"))); //输出初始化以后的多重映射mulmap: for(p=mulmap.begin();p!=mulmap.end();++p) cout<<(*p).first<<(*p).second<<endl; //检索并输出Jerry键所对应的所有的值 cout<<"find Jerry :"<<endl; p=mulmap.find(s("Jerry ")); while((*p).first=="Jerry ") { cout<<(*p).first<<(*p).second<<endl; ++p; } cin.get(); return 0; } //在map中是不允许一个键对应多个值的,在multimap中,不支持operator[],也就是说不支持map中允许的下标操作。
标签:
原文地址:http://www.cnblogs.com/aimqqroad-13/p/4770997.html