您的位置:首页 > 编程语言 > C语言/C++

c++ STL 之map 和 pair

2016-05-12 11:47 726 查看
pair在c++STL中被定义为struct。可以将两个值视为一个单元。经常在容器map中使用或用于函数有两个不同类型的返回值。

头文件是
#include <utility>


1. pair的定义:

template<class Type1, class Type2>
struct pair
{
typedef Type1 first_type;
typedef Type2 second_type
Type1 first;
Type2 second;
pair( );
pair(
const Type1& __Val1,
const Type2& __Val2
);
template<class Other1, class Other2>
pair(
const pair<Other1, Other2>& _Right
);
template<class Other1, class Other2>
pair(
Other1&& _Val1, Other2&& _Val2
);
};


2. pair的使用

(1)pair对象定义和初始化

// 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 );


(2)pair成员变量的访问

pair的两个变量名为first,second。直接用点操作变量名即可访问。

pair<string, string> a("Lily", "Poly");
string name;
name = pair.second;


(3)Pair 之间的比较

如果两个pair对象内的所有元素都相等,这两个pair对象就视为相等。第一个元素具有最高的优先级。所以第一个元素不相等,其比较结果就成为整个pair的比较结果。第一个元素相等再比较第二个元素。

3. map class

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


头文件
Header: <map>


1.key的值时独一无二的,用来自动排序。key的值是常数,不可以更改的。

2.对于排序准则而言,key必须是comparable(可比较的)

4. map的使用

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数据的插入

1.
map <int, int, less<int> > m1; //第三个参数为排序准则。逐个增大
m1.insert( pair<int,int>( 1, 10 ) );
m1.insert( pair<int,int>( 2, 20 ) );
2.
map<string,float> coll;
coll.insert(map<string,float>::value_type("otto",22.3));//value_type相当于pair
3.
coll["otto"] = 7.7;//m[key]返回一个reference,指向键值为key的元素。如果没有元素的key是"otto",便自动为map安插哟个新元素。,然后将7.7赋给value。
4.最方便的方法
coll.insert(make_pair("otto",22.3));


map中元素的删除:

map<string,float >::iterator pos;;
pos=coll.find(key);
if(pos==coll.end())
cout<<"we do not find key "<<endl;
else  coll.erase(pos);  //delete ;


map中 swap()

#include <map>
#include <iostream>

int main( )
{
using namespace std;
map <int, int> m1, m2, m3;
map <int, int>::iterator m1_Iter;
typedef pair <int, int> Int_Pair;

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

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

// This is the member function version of swap
//m2 is said to be the argument map; m1 the target map
m1.swap( m2 );    ///第一种使用
cout << "After swapping with m2, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout  << "." << endl;

// This is the specialized template version of swap
swap( m1, m3 );     ///第二种使用

cout << "After swapping with m3, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout   << "." << endl;
}
The original map m1 is: 10 20 30.
After swapping with m2, map m1 is: 100 200.
After swapping with m3, map m1 is: 300.

引自msdn
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pair map