C++ STL之map和multimap用法

一. Map与 Multimap 特点

Map 和 Multimap 将(key/value) 一组当做元素,它们可以根据 key 的排序准则自动将元素排序。


Multimap允许重复元素,map不允许重复

类型定义:

[cpp]  view plain copy
  1. typedef std::mapfloat, std::greater > StringFloatMap;  
  2.    ...  
  3. StringFloatMap coll;  

注意,两个>之间需加上一个空格,因为>>会被编译器视为移位操作符。

Map 和 multimap 通常以平衡二叉树来实现,map 和 multimap 拥有 set 和 multiset 所有能为和所有操作函数。

通常,不能直接改变key 的值,value 的值是可以直接修改的,因此可以想像元素的实质类型是 pair<constkey, T>。


二、在map和multimap中添加元素

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

4. map中元素的查找:

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

map::iteratorl_it;; 
l_it=maplive.find(112);//返回的是一个指针
if(l_it==maplive.end())
cout<<"we do not find112"<else

cout<<"wo find112"<

 

mapm;

if(m[112]=="")

cout<<"we do not find112"<

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

5. map中 swap的用法:
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
For example:
#include
#include

usingnamespace std;

int main()
{
map m1, m2, m3;
map ::iterator m1_Iter;

m1.insert( pair (1, 10 ) );
m1.insert ( pair  ( 2, 20 ) );
m1.insert ( pair  ( 3, 30 ) );
m2.insert ( pair  ( 10, 100 ) );
m2.insert ( pair  ( 20, 200 ) );
m3.insert ( pair  ( 30, 300 ) );

cout << "The original map m1is:";
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
cout << " "<second;
cout << "."<< endl;

// This isthe member function version of swap
// m2 is said to be theargument map; m1 the target map
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
cout << " "<< m1_Iter ->second;
cout << "."<< endl;


cout << "After swapping with m2, mapm2 is:";
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
cout << " "<< m1_Iter ->second;
cout << "."<< endl;


// This is the specialized template version of swap
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
cout << " "<< m1_Iter ->second;
cout << "."<< endl;
}

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

usingnamespace std;

int main( )
{
map m1;
map ::iterator m1_Iter;

m1.insert (pair  (1, 20 ) );
m1.insert ( pair ( 4, 40) );
m1.insert ( pair ( 3, 60) );
m1.insert ( pair ( 2, 50) );
m1.insert ( pair ( 6, 40) );
m1.insert ( pair ( 7, 30) );

cout<< "The original map m1is:"<for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
cout << m1_Iter->first<<""<second<
}

The original map m1 is:
1 20
2 50
3 60
4 40
6 40
7 30

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(k) 返回键值一个迭代器,该迭代器指向第一个键值不小于k的元素。

upper_bound(k) 最后一个大于等于k的位置,也是有一个新元素k进来时的插入位置

max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
value_comp() 返回比较元素value的函数

multimap
[cpp]  view plain  copy
 print ?
  1. //multimap允许重复的键值插入容器  
  2. //        **********************************************************        
  3. //        * pair只包含一对数值:pair                       *  
  4. //        * map是一个集合类型,永远保持排好序的,                   *  
  5. //  pair  * map每一个成员就是一个pair,例如:map           *  
  6. //        * map的insert()可以把一个pair对象作为map的参数,例如map

     *  

  7. //        ***********************************************************  
  8. #pragma warning(disable:4786)  
  9. #include  
  10. #include  
  11. using namespace std;  
  12.   
  13. int main(void)  
  14. {  
  15.  multimap<int,char*> m;  
  16.  //multimap的插入只能用insert()不能用数组  
  17.  m.insert(pair<int,char*>(1,"apple"));  
  18.     m.insert(pair<int,char*>(1,"pear"));//apple和pear的价钱完全有可能是一样的  
  19.  m.insert(pair<int,char*>(2,"banana"));  
  20.  //multimap的遍历只能用迭代器方式不能用数组  
  21.  cout<<"***************************************"<
  22.  multimap<int,char*>::iterator i,iend;  
  23.  iend=m.end();  
  24.  for(i=m.begin();i!=iend;i++)  
  25.  {  
  26.   cout<<(*i).second<<"的价钱是"  
  27.    <<(*i).first<<"元/斤\n";  
  28.  }  
  29.  cout<<"***************************************"<
  30.     //元素的反相遍历  
  31.  multimap<int,char*>::reverse_iterator j,jend;  
  32.  jend=m.rend();  
  33.  for(j=m.rbegin();j!=jend;j++)  
  34.  {  
  35.   cout<<(*j).second<<"的价钱是"  
  36.    <<(*j).first<<"元/斤\n";  
  37.  }  
  38.  cout<<"***************************************"<
  39.  //元素的搜索find(),pairequal_range(const key_type &k)const  
  40.     //和multiset的用法一样  
  41.  multimap<int,char*>::iterator s;  
  42.  s=m.find(1);//find()只要找到一个就行了,然后立即返回。  
  43.  cout<<(*s).second<<"    "  
  44.   <<(*s).first<
  45.  cout<<"键值等于1的元素个数是:"<
  46.  cout<<"***************************************"<
  47.  //删除 erase(),clear()  
  48.  m.erase(1);  
  49.     for(i=m.begin();i!=iend;i++)  
  50.  {  
  51.   cout<<(*i).second<<"的价钱是"  
  52.    <<(*i).first<<"元/斤\n";  
  53.  }  
  54.     return 0;  
  55. }  



你可能感兴趣的:(C++)