c++ STL 之map 和 pair
2016-05-12 11:47
726 查看
pair在c++STL中被定义为struct。可以将两个值视为一个单元。经常在容器map中使用或用于函数有两个不同类型的返回值。
头文件是
(2)pair成员变量的访问
pair的两个变量名为first,second。直接用点操作变量名即可访问。
(3)Pair 之间的比较
如果两个pair对象内的所有元素都相等,这两个pair对象就视为相等。第一个元素具有最高的优先级。所以第一个元素不相等,其比较结果就成为整个pair的比较结果。第一个元素相等再比较第二个元素。
头文件
1.key的值时独一无二的,用来自动排序。key的值是常数,不可以更改的。
2.对于排序准则而言,key必须是comparable(可比较的)
map数据的插入
map中元素的删除:
map中 swap()
头文件是
#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
相关文章推荐
- android Google Map获取地理位置信息的方法
- Spark RDD API详解(一) Map和Reduce
- Python中map()函数浅析
- Android使用Google Map浅谈
- Lua中ipair和pair的区别
- Erlang中的映射组Map详细介绍
- c++中map的基本用法和嵌套用法实例分析
- 百度地图给map添加右键菜单(判断是否为marker)
- JavaScript中实现Map的示例代码
- jquery中map函数与each函数的区别实例介绍
- jquery中map函数遍历数组用法实例
- jquery与google map api结合使用 控件,监听器
- jquery遍历数组与筛选数组的方法
- JavaScript中实现map功能代码分享
- 用json方式实现在 js 中建立一个map
- Android Map新用法:MapFragment应用介绍
- js Map List 遍历使用示例
- java实现遍历Map的方法
- velocity显示List与Map的方法详细解析
- Google Map Api和GOOGLE Search Api整合实现代码