STL泛型编程之map映照容器

  Map是STL的一个关联容器,它提供一对一的数据处理能力(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个称为该关键字的映照元素),由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,

  不允许插入相同的键值,比较函数只对元素的键值进行比较,使用时声明头文件#include<map>。

  1、map创建

1  map<string,float> m;     //创建

  2、插入元素的方法有三种

1 m.insert(pair<int, string>(1, "student_one")); 
2 m.insert(map<int, string>::value_type(2, "student_two")); 
3 m[3] = "student_three"; 

  3、元素的遍历的方法有三种

  第一种:应用前向迭代器。

1 map<int, string>::iterator  iter;
2 for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
3 {
4     Cout<<iter->first<<”   ”<<iter->second<<end;
5 }

  第二种:应用反相迭代器。可以使用反向迭代器reverse_iterator反向遍历map映照容器的数据,它需要rbegin()方法rend()方法指出反向遍历的起始位置和终止位置。

1 map<int,char>::reverse_iterator rit;
2 for(rit=m.rbegin();rit!=m.rend();rit++)
3 {
4 cout<<(*rit).first<<" : "<<(*rit).second<<endl;//输出键值与映照数据
5 }

  第三种:用数组方式。

1 int nSize = mapStudent.size()
2 for(int nIndex = 1; nIndex <= nSize; nIndex++) 
3 for(int nIndex = 0; nIndex < nSize; nIndex++)
4 {
5       Cout<<mapStudent[nIndex]<<end;
6 }

  4、元素的删除

  map映照容器的erase()删除元素函数,它有三个重载函数。可以删除某个迭代器位置上的元素、等于某个键值的元素,一个迭代器区间上的所有元素,当然,也可以使用clear()方法清空map映照容器。

  第一种:erase(map<T1,T2>::iterator iter),删除迭代器所指的节点。

  第二种:erase(key k),根据键值进行删除,删除键值k所指的节点 。

  第三种:erase(map<T1,T2>::iteratormap iter1,<T1,T2>::iteratoriter2),删除iter1和iter2之间的数据。

1  Map<int, string> mapStudent;
2        mapStudent.insert(pair<int, string>(1, “student_one”));
3        mapStudent.insert(pair<int, string>(2, “student_two”));
4        mapStudent.insert(pair<int, string>(3, “student_three”));
5        map<int, string>::iterator iter;
6        iter = mapStudent.find(1);
7        mapStudent.erase(iter);      //如果要删除1,用迭代器删除
8        Int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
9        mapStudent.earse(mapStudent.begin(), mapStudent.end());  //一下代码把整个map清空

   删除一个前闭后开的集合,这是STL的特性。

  5、元素的查找

  使用find()方法搜索某个键值,搜索到了,就返回该键值所在的迭代器的位置,否则,返回end()迭代器位置。搜索速度极快。 

 1 map<int,char>::iterator it;
 2 it=m.find(28);
 3 if(it!=m.end())//搜索到该键值
 4 {
 5 cout<<(*it).first<<" : "<<(*it).second<<endl;
 6 }
 7 else
 8 {
 9 cout<<"not found it"<<endl;
10 }

  注意输出的方式,循环访问迭代器。(*it).first指的是map容器的第一个值,(*it).second指的是map容器的第二个值。

  6、自定义比较函数

  将元素插入到map中去的时候,map会根据设定的比较函数将该元素放到该放的节点上去。在定义map的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值由小到大的顺序插入元素。在很多情况下,需要自己编写比较函数。

  编写函数的方法有两种:

  (1)如果元素不是结构体,那么,可以编写比较函数。

 1 #pragma warning(disable:4786)
 2 #include <map>
 3 #include <string>
 4 #include <iostream>
 5 using namespace std;
 6 //自定义比较函数myComp
 7 struct myComp
 8 {
 9 bool operator()(const int &a,const int &b)
10 {
11 if(a!=b)return a>b;
12 else
13 return a>b;
14 }
15 };
16 int main(int argc, char* argv[])
17 {
18 map<int,char,myComp> m;   //定义map 对象,当前没有任何元素
19 m[25]='m';
20 m[28]='k';
21 m[10]='x';
22 m[30]='a';    //插入元素,按键值的由小到大放入黑白树中
23 map<int,char,myComp>::iterator it;   //使用前向迭代器中序遍历map
24 for(it=m.begin();it!=m.end();it++)
25 {
26 cout<<(*it).first<<" : "<<(*it).second<<endl;
27 }
28 return 0;
29 }

  运行结果:

1 30 : a
2 28 : k
3 25 : m
4 10 : x

  (2)如果元素是结构体,那么,可以直接把比较函数写在结构体内。

 1 struct Info
 2 {
 3 string name;
 4 float score;
 5 bool operator < (const Info &a) const          //重载“<”操作符,自定义排序规则
 6 {
 7 return a.score<score;        //按score 由大到小排列。如果要由小到大排列,使用“>”号即可
 8 }
 9 };
10 int main(int argc, char* argv[])
11 {
12 map<Info,int> m;       //定义map 对象,当前没有任何元素
13 Info info;           //定义Info 结构体变量
14 info.name="Jack";
15 info.score=60;               //插入元素,按键值的由小到大放入黑白树中
16 m[info]=25;
17 info.name="Bomi";
18 info.score=80;
19 m[info]=10;
20 info.name="Peti";
21 info.score=66.5;
22 m[info]=30;
23 map<Info,int>::iterator it;         //使用前向迭代器中序遍历map
24 for(it=m.begin();it!=m.end();it++)
25 {
26 cout<<(*it).second<<" : ";
27 cout<<((*it).first).name<<" "<<((*it).first).score<<endl;
28 }
29 return 0;
30 }

  运行结果为:

1 10 : Bomi 80
2 30 : Peti 66.5
3 25 : Jack 60

   7、数据的清空与判空
  清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map。

  8、map基本操作函数

 1       C++ Maps是一种关联式容器,包含“关键字/值”对 
 2       begin()          返回指向map头部的迭代器 
 3       clear()         删除所有元素 
 4       count()          返回指定元素出现的次数 
 5       empty()          如果map为空则返回true 
 6       end()            返回指向map末尾的迭代器 
 7       equal_range()    返回特殊条目的迭代器对 
 8       erase()          删除一个元素 
 9       find()           查找一个元素 
10       get_allocator()  返回map的配置器 
11       insert()         插入元素 
12       key_comp()       返回比较元素key的函数 
13       lower_bound()    返回键值>=给定元素的第一个位置 
14       max_size()       返回可以容纳的最大元素个数 
15       rbegin()         返回一个指向map尾部的逆向迭代器 
16       rend()           返回一个指向map头部的逆向迭代器 
17       size()           返回map中元素的个数 
18       swap()            交换两个map 
19       upper_bound()     返回键值>给定元素的第一个位置 
20       value_comp()      返回比较元素value的函数 

 

转载于:https://www.cnblogs.com/suvllian/articles/5367746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值