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;
}
}
- 输出