c++ map 自定义数据类型进行排序

本文详细介绍了如何在C++中对map进行自定义排序,包括默认排序规则、自定义数据类型排序以及按value值排序。通过示例代码展示了如何使用比较函数对象(comparator)来改变map的排序方式,并通过将map元素转换为vector进行排序。此外,还讨论了sort算法对线性容器的限制及其解决方法。
摘要由CSDN通过智能技术生成

map自定义数据类型进行排序

1. 默认排序

  • 按key值从小到大排序,底层实现是红黑树。
// map 的内置数据类型 
void func3()
{
	map<int, string> mp; // 默认是 less<int> 从小到大排序
	// map<int, string> mp; // greater<int> 从大到小排序
	mp[5] = "aaa";
	mp[2] = "ccc";
	mp[1] = "lll";
	mp[8] = "bbb";

	map<string, int> mp2;  // 默认是 less<string> 从小到大排序
	// map<string, int, greater<string>> mp2;  // greater<string> 从大到小排序
	mp2["aaa"] = 5;
	mp2["ccc"] = 2;
	mp2["lll"] = 1;
	mp2["bbb"] = 8;


	for (auto m : mp)
	{
		cout << m.first << " " << m.second << endl;
	}
	cout << "--------------------------------" << endl;
	for (auto m : mp2)
	{
		cout << m.first << " " << m.second << endl;
	}

}
  • 默认从小到大
    在这里插入图片描述

  • greator< type > 从大到小

在这里插入图片描述

2. 自定义数据类型排序

// map 的自定义数据类型排序
struct comp3
{
	/* data */
	bool operator()(vector<int> const &a, vector<int> const &b) const  // 这里的const一定不能省略
	{																   // 省略了会报错,但是我不知道为什么
		return a[0] < b[0];  // 按v[0]进行从小到大排序  
		// return a[1] < b[1]; // 按v[1]进行从小到大排序  
	}
};

void func3()
{
	map<vector<int>, int, comp3> mp3;
	mp3[{5, 1}] = 1;
	mp3[{3, 2}] = 2;
	mp3[{1, 6}] = 3;
	mp3[{4, 3}] = 4;
	mp3[{6, 4}] = 5;

	for (auto m : mp3)
	{
		cout << m.first[0] << " and " << m.first[1] << " " << m.second << endl;
	}
}
  • 按v[0]进行从小到大排序
    在这里插入图片描述

  • 按v[1]进行排序
    在这里插入图片描述

3. 按 value 值进行排序

struct comp3
{
	/* data */
	bool operator()(vector<int> const &a, vector<int> const &b) const  // 这里的const一定不能省略
	{																   // 省略了会报错,但是我不知道为什么
		return a[1] < b[1];
	}
};

// map 按value排序
void func4()
{
	map<int, string, greater<int>> mp;
	mp[5] = "aaa";
	mp[2] = "ccc";
	mp[1] = "lll";
	mp[8] = "bbb";

	map<string, int, greater<string>> mp2;
	mp2["aaa"] = 5;
	mp2["ccc"] = 2;
	mp2["lll"] = 1;
	mp2["bbb"] = 8;

	map<vector<int>, int, comp3> mp3;
	mp3[{5, 1}] = 9;
	mp3[{3, 2}] = 6;
	mp3[{1, 6}] = 7;
	mp3[{4, 3}] = 4;
	mp3[{6, 4}] = 5;

	map<int, vector<int>> mp4;
	mp4[1] = {5, 1};
	mp4[2] = {3, 2};
	mp4[3] = {1, 6};
	mp4[4] = {4, 3};
	mp4[5] = {6, 4};

	//  第一反应是利用stl中提供的sort算法实现,这个想法是好的,不幸的是,sort算法有个限制,利用sort算法只能对线性容器进行排序(如vector,list,deque)。
	//  map是一个集合容器,它里面存储的元素是pair,不是线性存储的
	//  迂回一下,把map中的元素放到序列容器(如vector)中,然后再对这些元素进行排序。

	vector<pair<int, string>> mpv(mp.begin(), mp.end());
	vector<pair<string, int>> mp2v(mp2.begin(), mp2.end());
	vector<pair<vector<int>, int>> mp3v(mp3.begin(), mp3.end());
	vector<pair<int, vector<int>>> mp4v(mp4.begin(), mp4.end());
	sort(mpv.begin(), mpv.end(), [](pair<int, string> &a, pair<int, string> &b){
		return a.second < b.second;  // 进行正序排序
	});
	sort(mp2v.begin(), mp2v.end(), [](pair<string, int> &a, pair<string, int> &b){
		return a.second < b.second;  // 进行正序排序
	});
	sort(mp3v.begin(), mp3v.end(), [](pair<vector<int>, int> &a, pair<vector<int>, int> &b){
		return a.second < b.second;  // 进行正序排序
	});
	sort(mp4v.begin(), mp4v.end(), [](pair<int, vector<int>> &a, pair<int, vector<int>> &b){
		return a.second[0] < b.second[0]; // 按 value(也就是vector)的第一个数进行正序排序
	});


	cout << "mpv: " << endl;
	for (auto m : mpv)
	{
		cout << m.first << " " << "value: " << m.second << endl;
	}
	cout << "--------------------------------" << endl;
	cout << "mp2v: " << endl;
	for (auto m : mp2v)
	{
		cout << m.first << " " << "value: "<< m.second << endl;
	}
	cout << "--------------------------------" << endl;
	cout << "mp3v: " << endl;
	for (auto m : mp3v)
	{
		cout << m.first[0] << " and " << m.first[1] << " " << "value: " << m.second << endl;
	}
	cout << "--------------------------------" << endl;
	cout << "mp4v: " << endl;
	for (auto m : mp4v)
	{
		cout << m.first << " " << "value: " << m.second[0] << " and "  << m.second[1] << endl;
	}
}
  • 输出
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值