摘自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中已经存在,插入就不成功。
本文详细介绍了C++ STL中的map容器,包括其内部实现原理、基本操作及使用示例。通过具体的代码实例展示了如何创建和使用map,以及如何利用pair进行元素的插入与查找。

1378

被折叠的 条评论
为什么被折叠?



