C++ STL容器(五)之map

map是C++ STL中的一种有序关联映射容器,map容器中的元素的数据组成为键值和其对应的数据(称键值对),通过pair封装成结构对象。map的数据组织方式同set一样,也是通过红黑树(Red-Black Tree)实现。
使用时需使用宏语句:

#include<map>

一.几种创建map的方式:

    1.用默认的less<T>函数对象(即元素键值按照从小到大的顺序)创建空map;
	map<int,char>mp1;
	
	2.自定义比较函数创建map;
	struct comp{
	    bool operator()(const int a,const int b)
     	{
	    	return a>b;
	    }
    };//要放在主函数之外独立存在;
	map<int,char,comp>mp2;
	
	3.拷贝另一个map创建新的map;
	map<int,char>mp3(mp1);
	
	4.迭代器区间复制(注意map中存的对象为pair);
	pair<int,char> p1(1,'a');
	pair<int,char> p2(2,'b');
	pair<int,char> p3(3,'c');
	pair<int,char> p[]={p1,p2,p3};
	map<int,char> mp4(p,p+3);
	
	5.迭代器区间复制+自定义比较函数;
	pair<int,char> p4(4,'x');
	pair<int,char> p5(5,'y');
	pair<int,char> p6(6,'z');
	pair<int,char> pp[]={p4,p5,p6}; 
	map<int,char,comp>mp5(pp,pp+3);

二.插入/删除/检索元素:

    map<int,char>mp;
	
	1.插入键值/数据对;
	(1)通过数组下标操作符'[]'插入;
	mp[1]='a';
	mp[2]='b';
	mp[3]='c';
	
	(2)通过insert函数插入(插入的是一个pair对象);
	mp.insert(make_pair(4,'d'));
	mp.insert(make_pair(5,'e'));
    
    2.删除键值为value的键值对(或删除迭代器区间[first,last)上的所有键值对);
	mp.erase(5);

    3.搜索键值为value的元素(若不存在返回迭代器end);
    if(mp.find(1)!=mp.end()) cout<<"键值为1的元素在map中"<<endl;
	else cout<<"键值为1的元素不在map中"<<endl;

三.迭代遍历(正向/反向)

    迭代遍历(正向/反向),注意map中的元素对象是以pair的形式封装的;
	1.正向迭代遍历; 
	map<int,char>::iterator i;
	cout<<"正向迭代遍历:"<<endl;
	for(i=mp.begin();i!=mp.end();i++)
	{
		cout<<(*i).first<<" "<<(*i).second<<endl;
	}
	cout<<endl; 
	
	2.反向迭代遍历;
	map<int,char>::reverse_iterator ri;
	cout<<"反向迭代遍历:"<<endl;
	for(ri=mp.rbegin();ri!=mp.rend();ri++)
	{
		cout<<(*ri).first<<" "<<(*ri).second<<endl;
	}
	cout<<endl; 

四.其他常用操作:

    1.返回map的键值对数;
	cout<<"map的键值对数量为:"<<mp.size()<<endl;
	
    2.通过键值访问其对应的数据;
	cout<<"键值为1的元素对应的数据为:"<<mp[1]<<endl; 
	
	3.清空map;
	mp.clear();
	
	4.判断map是否为空;
	if(mp.empty()) cout<<"map为空"<<endl;
	else cout<<"map不空"<<endl;

五.测试代码:

#include<cstdio>
#include<map>
#include<iostream>
using namespace std;
struct comp{
	bool operator()(const int a,const int b)
	{
		return a>b;
	}
};
int main()
{
	/*几种创建map的方式;
	//1.用默认的less<T>函数对象创建空map;
	map<int,char>mp1;
	
	//2.自定义比较函数创建map;
	map<int,char,comp>mp2;
	
	//3.拷贝另一个map创建新的map;
	map<int,char>mp3(mp1);
	
	//4.迭代器区间复制(注意map中存的对象为pair);
	pair<int,char> p1(1,'a');
	pair<int,char> p2(2,'b');
	pair<int,char> p3(3,'c');
	pair<int,char> p[]={p1,p2,p3};
	map<int,char> mp4(p,p+3);
	
	//5.迭代器区间复制+自定义比较函数;
	pair<int,char> p4(4,'x');
	pair<int,char> p5(5,'y');
	pair<int,char> p6(6,'z');
	pair<int,char> pp[]={p4,p5,p6}; 
	map<int,char,comp>mp5(pp,pp+3);
	*/
	
	map<int,char>mp;
	
	//插入键值/数据对;
	//1.通过数组下标操作符'[]'插入;
	mp[1]='a';
	mp[2]='b';
	mp[3]='c';
	
	//2.通过insert函数插入(插入的是一个pair对象);
	mp.insert(make_pair(4,'d'));
	mp.insert(make_pair(5,'e'));
	
	//迭代遍历(正向/反向);
	//1.正向迭代遍历; 
	map<int,char>::iterator i;
	cout<<"正向迭代遍历:"<<endl;
	for(i=mp.begin();i!=mp.end();i++)
	{
		cout<<(*i).first<<" "<<(*i).second<<endl;
	}
	cout<<endl; 
	
	//2.反向迭代遍历;
	map<int,char>::reverse_iterator ri;
	cout<<"反向迭代遍历:"<<endl;
	for(ri=mp.rbegin();ri!=mp.rend();ri++)
	{
		cout<<(*ri).first<<" "<<(*ri).second<<endl;
	}
	cout<<endl; 
	
	//删除键值为value的键值对(或删除迭代器区间[first,last)上的所有键值对);
	mp.erase(5);
	cout<<"删除键值为5的元素后的遍历结果:"<<endl;
	for(i=mp.begin();i!=mp.end();i++)
	{
		cout<<(*i).first<<" "<<(*i).second<<endl;
	}
	cout<<endl;
	
	//搜索键值为value的元素(若不存在返回迭代器end);
	if(mp.find(5)!=mp.end()) cout<<"键值为5的元素在map中"<<endl;
	else cout<<"键值为5的元素不在map中"<<endl; 
	
	if(mp.find(1)!=mp.end()) cout<<"键值为1的元素在map中"<<endl;
	else cout<<"键值为1的元素不在map中"<<endl;
	
	//返回map的键值对数;
	cout<<"map的键值对数量为:"<<mp.size()<<endl;
	
	//通过键值访问其对应的数据;
	cout<<"键值为1的元素对应的数据为:"<<mp[1]<<endl; 
	
	//清空map;
	mp.clear();
	
	//判断map是否为空;
	cout<<"清空map后:"; 
	if(mp.empty()) cout<<"map为空"<<endl;
	else cout<<"map不空"<<endl;
	
	return 0;
}

测试结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值