STL的pair学习, map学习

时间:2023-03-08 20:35:30
STL的pair学习, map学习

http://blog.csdn.net/calvin_zcx/article/details/6072286

http://www.linuxidc.com/Linux/2014-10/107621.htm

头文件  : <utility>

pair 的  <、>、<=、>=、==、!= 的比较规则  :  先比较first,first相等时再比较second   (可以通过重载这几个运算符来重新指定自己的比较逻辑)

pair的初始化:   pair<string, int> prt;        pair<string, int>pr2("hello", 5);   pair<string, int> pr3 = make_pair<string, int>("haha", 4);    pair<stirng, int>pr4 = make_pair("lll",3);

pair和vector交互:  pair<string, vector<int>> student;

pair访问元素:   pair<int, int> a(1,2);

cout<<"first="<<a.first<<"------"<<"second="<<a.second;

pair使用typedef技巧:   typedef pair<string, int>   nameInfo;

nameInfo info("lucy", 4);

pair与标准输入流:   pair<string, string> input;

while(cin>>input.first>>input.second){ cout<<"info is"<<input.first<<":"<<input.second<<endl;}

          (abc[enter]d---->info is abc:d)

pair的隐式转换:      pair可以接受隐式的类型转换,这样可以获得更高的灵活度。但是这样会出现如下问题:例如有如下两个定义:

          pair<int, float>(1, 1.1);
          make_pair(1, 1.1);                 make_pair函数会将second变量都转换成double类型.这个问题在编程是需要引起注意。

          make_pair<int, float>(1,1.1);   如果想指定是float的可以像这样指明类型.

vector套用pair的一个小例子:

STL的pair学习, map学习STL的pair学习, map学习

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

继续学习map

http://www.360doc.com/content/12/0417/16/3349869_204420932.shtml

http://www.cnblogs.com/kevintian/articles/1277700.html

map的介绍

首先是map的模板头的学习

STL的pair学习, map学习

map内部存储机制实际是以红黑树为基础,红黑树在插入节点时,必须依照大小比对之后在一个合适的位置上执行插入动作。所以作为关键字,起码必须有“<”这个比较操作符。我们知道,int,float,enum,size_t等等简单关键字,都有内置的比较函数,与map搭配无论是插入还是查找,都没什么问题。但是作为复杂数据类型,如果没有明确定义“<”比较操作符,就不能与map直接搭配使用,除非我们自己定义第三个参数。

在选择map的关键字时,注意以下两点,同时这两点也是改错的方法:

a) 关键字明确定义“<”比较操作符

b) 没有“<”比较操作符,自定义仿函数替代第三个参数Compare,该仿函数实现“()”操作符,提供比较功能。插入时各节点顺序以该仿函数为纲。

STL的pair学习, map学习

如果TwoNum类提供了 bool operator < (const TwoNum& a) 函数的话,可以不用定义Compare类.main里面的map在声明myMap的时候可以写成 map<TwoNum, int> .  而如果TwoNum类没提供对于<运算符的重载的话,那么必须提供一个类,这个类里面重载了()运算符.而()运算符负责对TwoNum进行比较.

但是单纯的重载<符合是错误的.例如下例:

STL的pair学习, map学习  STL的pair学习, map学习 STL的pair学习, map学习

接来下学习一下map增加元素:

(1). 用insert函数插入pair数据

 map<int, string> mapStu;
mapStu.insert(pair<int, string>(,"student_lilei"));
mapStu.insert(pair<int, string>(, "student_zhanffei"));

第一种方法和第二种方法效果上完全相同.

用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,

即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值.

用程序说明:

mapStudent.insert(map<int, string>::value_type (1, "student_one"));

mapStudent.insert(map<int, string>::value_type (1, "student_two"));

上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,

那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下

pair<map<int, string>::iterator, bool> Insert_Pair;

Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));

我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。

   
   

(2).用insert函数插入value_type数据

(

 map<int, string> mapStu;
mapStu.insert(map<int, string>::value_type(,"stu_lilei"));
mapStu.insert(map<int, string>::value_type(,"stu_lucy"));
3).用数组方式插入数据
 map<int, string> mapStu;
mapStu[] = "stu_lilei";
mapStu[] = "stu_lucy";
//注意 用 下标的方式[]插入的的时候如果这个key不存在,那么会经历的步骤是
//1.创建key和key对应的空的value
//2.把真正的value赋值给这个key
class A
{
public:
A(){}
int i = ;
}; A a;
a.i = ;
map[] = a; 上面的具体过程是
() 1不在map中 创建一个 :A() {注意空的A对象}
() 1在map中了 把a赋值给key是1的value
这样就存在一个性能问题 因为你是先构建A()然后再做了一次赋值,这样的结果是如果类对象比较复杂的话,性能就不如用insert好了..切记注意啊.

写个小例子:

STL的pair学习, map学习  STL的pair学习, map学习

继续深入的学习map

http://blog.csdn.net/zhoujiaxq/article/details/9786551

映射和多重映射基于某一类型Key的键集的存在,提供对T类型的数据进行快速和高效的检索。对map而言,键只是指存储在容器中的某一成员。Map不支持副本键,multimap支持副本键。Map和multimap对象包涵了键和各个键有关的值,键和值的数据类型是不相同的,这与set不同。set中的key和value是Key类型的,而map中的key和value是一个pair结构中的两个分量。

map的构造函数

 Template<class T1, class T2>
map(); //默认构造函数
map(const map& m); //拷贝构造函数
map(iterator begin, iterator end); //区间构造函数
map(iterator begin, iterator end, const traits& _compare); //带比较谓词的构造函数
map(iterator begin, iterator end, const traits& _compare, const allocator& all); //带分配器

map的嵌套定义

map<string, map<string, long> > //注意,最后的两个>直接有个空格

map的访问

map支持下标运算符operator[], 可以用访问普通数组的方式来访问map,但是[]里面的值不能是下标0,1,2,3,4而是key值 : value = map[key]

查找并获取map中元素

(1)直接用key获取   value = map[key]  . 这样做的风险是:如果这个key不存在的话,会自动插入一个实力,value值为初始化值

(2)使用find()或者count()方法来探视某个key是否存在

 if (map.find(key) != map.end())

 {
cout<<''找到啦"<<end;
}

从map中删除元素

 //删除某个key-value
iterator erase(iterator it);
size_type erase(const Key& key);
iterator erase(iterator first, iterator last); //删除所有元素
clear();
map.erase(map.begin(), map.end());

map中swap的用法

 //map中的swap不是一个容器中的元素交换,而是两个容器交换

 #include <map>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
map<int, int> m1, m2;
map<int, int>::iterator iter;
m1.insert(pair<int,int>(,)); m1[] = ;
m1.insert(make_pair<int, int>(,));
m1.insert(make_pair(,)); m2[] = ;
m2[] = ; m1.swap(m2); cout<<"new m1:"<<endl<<endl;
for (iter = m1.begin(); iter != m1.end(); iter++)
{
cout<<iter->first<<":"<<iter->second<<endl;
} cout<<"new m2:"<<endl;
for (iter = m2.begin(); iter != m2.end(); iter++)
{
cout<<iter->first<<":"<<iter->second<<endl;
}
return ;
}

STL的pair学习, map学习

map按照value进行排序的sort问题 http://blog.csdn.net/flybywind/article/details/7536311

我们知道对map用key进行排序的话比较容易,但是如果碰到了需要对map按照value进行排序的时候怎么办么?

 //STL的sort函数原型
#include <algorithm>
using namespace std; template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

显然sort函数需要一个随机迭代器, 而这对于map对象来说是不可能的.所以我们必须把key-value抽取出来,放到一个vector对象里才行.

 #include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#include<utility> using namespace std; typedef pair<int, int> key_value; bool comp(key_value pair1, key_value pair2)
{
return pair1.second < pair2.second;
} void printVec( key_value pair1)
{
cout<<pair1.first<<":"<<pair1.second<<endl;
} int main()
{
map< int, int, std::less<int> > test;
test[] = ;
test[] = ;
test[] = ;
test[] = ;
test[] = ; vector<key_value> key_values;
for (auto iter = test.rbegin(); iter != test.rend(); iter++)
{
int key = iter->first;
int value = iter->second;
//key_values.push_back( make_pair<int,int>(key, value) );
key_values.push_back( make_pair(key, value) );
} cout<<"before sort"<<endl;
for_each(key_values.begin(), key_values.end(), printVec); sort(key_values.begin(), key_values.end(), comp); cout<<"after sort"<<endl;
for_each(key_values.begin(), key_values.end(), printVec); return ;
}

STL的pair学习, map学习