c++ stl--map

 

摘自MSDN,IDE为VS2012

template <
   class Key, 
   class Type, 
   class Traits = less<Key>, 
   class Allocator=allocator<pair <const Key, Type> > 
>
class map

map是关联式容器,所有的元素都是pair。自动建立key-value关系。在头文件#include<map>中。


stl中pair定义:

   struct pair 
   {
   typedef Type1 first_type;
   typedef Type2 second_type;
   Type1 first;
   Type2 second;
   pair( );
   pair(
      const Type1& __Val1, 
      const Type2& __Val2
   );

 

pair的第一个元素是key,第二个元素是value。

map底层是以RB-TREE为机制,RB-TREE是一种平衡二叉搜索树,自动排序效果不错。

map<type1,type2> m[pair.first]=pair.second;并且所有的pair.first在该map中唯一的

例子:

#include <iostream>
#include <map>
#include <string>


using namespace std;


void main()
{
map<int,size_t> VertexOnConflictFacet;
VertexOnConflictFacet[0];//第0个pair是(0,0),未初始化默认为0
VertexOnConflictFacet[2]++;//第1个pair是(2,1)
VertexOnConflictFacet[2]++;//第1个pair变为(2,2)
VertexOnConflictFacet[5]++;//第2个pair是(5,1)
VertexOnConflictFacet[6]=10;//第3个pair是(6,10)
VertexOnConflictFacet[6]=40;//第3个pair变为(6,40)


map<string,int> simap;
simap[string("zhao")]=1;//第0个pair是("zhao",1)
simap[string("qian")]=2;//第1个pair是("qian",2)
simap[string("qian")]=23;//第1个pair变为("qian",23)
}

msdn例子:

// utility_pair.cpp
// compile with: /EHsc
#include <utility>
#include <map>
#include <iomanip>
#include <iostream>

int main( )
{
   using namespace std;

   // Using the constructor to declare and initialize a pair
   pair <int, double> p1 ( 10, 1.1e-2 );

   // Compare using the helper function to declare and initialize a pair
   pair <int, double> p2;
   p2 = make_pair ( 10, 2.22e-1 );

   // Making a copy of a pair
   pair <int, double> p3 ( p1 );

   cout.precision ( 3 );
   cout << "The pair p1 is: ( " << p1.first << ", " 
        << p1.second << " )." << endl;
   cout << "The pair p2 is: ( " << p2.first << ", " 
        << p2.second << " )." << endl;
   cout << "The pair p3 is: ( " << p3.first << ", " 
        << p3.second << " )." << endl;

   // Using a pair for a map element
   map <int, int> m1;
   map <int, int>::iterator m1_Iter;

   typedef pair <int, int> Map_Int_Pair;

   m1.insert ( Map_Int_Pair ( 1, 10 ) );
   m1.insert ( Map_Int_Pair ( 2, 20 ) );
   m1.insert ( Map_Int_Pair ( 3, 30 ) );

   cout << "The element pairs of the map m1 are:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " ( " << m1_Iter -> first << ", "
           << m1_Iter -> second << " )";
   cout   << "." << endl;

   // Using pair as a return type for a function
   pair< map<int,int>::iterator, bool > pr1, pr2;
   pr1 = m1.insert ( Map_Int_Pair ( 4, 40 ) );
   pr2 = m1.insert ( Map_Int_Pair (1, 10 ) );

   if( pr1.second == true )
   {
      cout << "The element (4,40) was inserted successfully in m1."
           << endl;
   }
   else   
   {
      cout << "The element with a key value of\n"
           << " ( (pr1.first) -> first ) = " << ( pr1.first ) -> first 
           << " is already in m1,\n so the insertion failed." << endl;
   }

   if( pr2.second == true )
   {
      cout << "The element (1,10) was inserted successfully in m1."
           << endl;
   }
   else   
   {
      cout << "The element with a key value of\n"
           << " ( (pr2.first) -> first ) = " << ( pr2.first ) -> first 
           << " is already in m1,\n so the insertion failed." << endl;
   }
}
结果:

The pair p1 is: ( 10, 0.011 ).
The pair p2 is: ( 10, 0.222 ).
The pair p3 is: ( 10, 0.011 ).
The element pairs of the map m1 are: ( 1, 10 ) ( 2, 20 ) ( 3, 30 ).
The element (4,40) was inserted successfully in m1.
The element with a key value of
 ( (pr2.first) -> first ) = 1 is already in m1,
 so the insertion failed.


map的insert( )函数,实际是调用RB-TREE底层的insert_unique( )函数,返回pair。该piar.first=要插入的原来pair,pair.second是bool类型,表示是否插入成功。

如果要插入的pair.fisrt在map中已经存在,插入就不成功。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值