map__

map的内部实现是红黑树 ,是有序储存的,默认是依照key值由小到大; 

如下代码::

输入::

8
2
4
2
4
5
100
2
100

输出::

2 3
4 2
5 1
100 2
#include<iostream>
#include<map>

using namespace std;

int main()
{
	map<int, int>ma;

	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		ma[x]++;
	}
	
	for (map<int, int>::iterator it = ma.begin(); it != ma.end(); it++)
		cout <<it->first << ' ' << it->second << endl;

	return 0;
}

 map可以指定第三个参数进行自定义排序

#include<iostream>
#include<string>
#include<map>
using namespace std;

struct cmp
{
	bool operator()(const string& str1, const string& str2)
	{
		//按string的长度进行排序,长度相同时,比较字符串的大小
		return str1.length() < str2.length() || str1 < str2;
	}

};

int main()
{
	//map的第三个参数是类对象
	map<string, int, cmp> mp;
	mp["Liming"] = 90;
	mp["Xiaohong"] = 85;
	mp["Wangge"] = 95;
	mp.insert(make_pair("Bob", 60));
	map<string, int>::iterator iter;
	for (iter = mp.begin(); iter != mp.end(); iter++)
		cout << iter->first << "\t" << iter->second << endl;
	return 0;
}

 map依照value值进行排序

sort()函数只能对线性的进行排序,比如vector,list,deque,但是map是一个集合容器,里面储存的数据是pair,他不是线性储存,但是可以将map中的数据放到有序容器中(比如vector)中,然后在对其中的数据进行排序

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;



bool cmp(pair<string, int> a, pair<string, int> b)
{
	return a.second < b.second;//从小到大
}

int main()
{
	//map的第三个参数是类对象
	map<string, int> mp;
	mp["Liming"] = 90;
	mp["Xiaohong"] = 85;
	mp["Wangge"] = 95;
	mp.insert(make_pair("Bob", 60));

	//数据转移
	vector< pair<string, int> > vec;
	for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) 
		vec.push_back(pair<string, int>(it->first, it->second));

	//排序
	sort(vec.begin(), vec.end(), cmp);
	//输出
	for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++)
		cout << it->first << ' ' << it->second <<  endl;

	return 0;
}

map 基础操作 

#include<iostream>
#include<map>

using namespace std;

void text01()
{
	map<int, int>mymap;

	//map插入输出方式
	//插入数据,first是key值,second是value值
	//第一种
	//mymap.insert(pair<int, int>(10, 10));
	pair<map<int, int>::iterator, bool> ret = mymap.insert(pair<int, int>(10, 10));
	if (ret.second)
		cout << "第一次插入成功" << endl;
	else
		cout << "第一次插入失败" << endl;
	cout << ".............." << endl;

	ret = mymap.insert(pair<int, int>(10, 20));
	if (ret.second)
		cout << "第二次插入成功" << endl;
	else
		cout << "第二次插入失败" << endl;
	cout << ".............." << endl;

	//第二种
	mymap.insert(make_pair(20, 20));
	//第三种
	mymap.insert(map<int, int>::value_type(30, 30));
	//第四种!!!!
	mymap[40] = 40;

	//更新map中的value值
	mymap[10] = 999999;

	//打印//iterator有两种输出方式
	//*it取出的是pair

	for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++)
		cout <<"key:::" <<it->first << ' ' <<"value::"<< (*it).second << endl;

	//如果通过  [ ]  访问 map 中一个不存在的 key 值,那么  map  会将将这一个key 
	//插入到map 中,并且会给value 一个默认值
	cout <<"mymap[60]:::"<< mymap[60] << endl;

	for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++)
		cout << "key:::" << it->first << ' ' << "value::" << (*it).second << endl;

	return;
}
int main()
{
	text01();

	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值