一.map键(key)的排序
map中有默认排序,它里面的构造是用到红黑树,所以它的默认排序是按照键来排序的,并且是按照键的升序来排序的。
(1) map中key的默认排序
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
map<int,string>m;
int main()
{
m[1] = "abs";
m[6] = "emxs";
m[3] = "bd";
m[4] = "awf";
for(auto it = m.begin(); it != m.end(); it++ )
cout << it->first << " " << it->second << endl;
return 0;
}
/*
输出:
1 abs
3 bd
4 awf
6 emxs
*/
(2)对于key的自定义排序
map中的第三个参数其实就是排序规则,不写就会默认成默认排序
(1)key(键)是int型
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
struct cmp{
bool operator()(int a, int b){
return a > b; //对于键是int型按照从大到小排
}
};
map<int, string, cmp>m; //map中的第三个参数其实就是排序规则,不写就会默认成默认排序
int main()
{
m[1] = "abs";
m[6] = "emxs";
m[3] = "bd";
m[4] = "awf";
for(auto it = m.begin(); it != m.end(); it++ )
cout << it->first << " " << it->second << endl;
return 0;
}
/*
输出:
6 emxs
4 awf
3 bd
1 abs
*/
(2)key(键)是string型
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
struct cmp{
bool operator()(string a, string b){
return a > b; //对于键是string型按照从大到小排
}
};
map<string, int, cmp>m;
int main()
{
m["zac"] = 22;
m["abc"] = 122;
m["cb"] = 12;
m["acd"] = 99;
m["emxs"] = 66;
for(auto it = m.begin(); it != m.end(); it++ )
cout << it->first << " " << it->second << endl;
return 0;
}
/*
输出:
zac 22
emxs 66
cb 12
acd 99
abc 122
*/
(3)key(键)是struct(结构体)型
第一种
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef struct{
string name;
int score;
}emxs;
struct rule{
bool operator()(emxs a,emxs b){ //按成绩从大到小排序,成绩相同按名字降序
if(a.score == b.score){
return a.name > b.name;
}
return a.score > b.score;
}
};
int main()
{
emxs stu;
map<emxs, int, rule> m;
stu.name = "abc";
stu.score = 88;
m[stu] = 10; //学号
stu.name = "acd";
stu.score = 95;
m[stu] = 22;
stu.name = "bcbc";
stu.score = 88;
m[stu] = 24;
stu.name = "emxs";
stu.score = 100;
m[stu] = 8;
map<emxs, int, rule>::iterator it; //或换为auto
for(it = m.begin(); it != m.end(); it++ )
cout << "名字:" << it->first.name << " 成绩:" << it->first.score << " 学号:" << it->second << endl;
/*
for(auto it = m.begin(); it != m.end(); it++ )
cout << "名字:" << it->first.name << " 成绩:" << it->first.score << " 学号:" << it->second << endl;
*/
return 0;
}
/*
输出:
名字:emxs 成绩:100 学号:8
名字:acd 成绩:95 学号:22
名字:bcbc 成绩:88 学号:24
名字:abc 成绩:88 学号:10
*/
第二种
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef struct emxs{
string name;
int score;
bool operator <(const emxs &s) const{ //按成绩从大到小排序,成绩相同按名字降序
if(score != s.score){
return score > s.score;
}
return name > s.name;
}
}emxs;
int main()
{
emxs stu;
map<emxs, int> m;
stu.name = "abc";
stu.score = 88;
m[stu] = 10; //学号
stu.name = "acd";
stu.score = 95;
m[stu] = 22;
stu.name = "bcbc";
stu.score = 88;
m[stu] = 24;
stu.name = "emxs";
stu.score = 100;
m[stu] = 8;
map<emxs, int>::iterator it; //或换为auto
for(it = m.begin(); it != m.end(); it++ )
cout << "名字:" << it->first.name << " 成绩:" << it->first.score << " 学号:" << it->second << endl;
/*
for(auto it = m.begin(); it != m.end(); it++ )
cout << "名字:" << it->first.name << " 成绩:" << it->first.score << " 学号:" << it->second << endl;
*/
return 0;
}
/*
输出:
名字:emxs 成绩:100 学号:8
名字:acd 成绩:95 学号:22
名字:bcbc 成绩:88 学号:24
名字:abc 成绩:88 学号:10
*/
二.map值(value)的排序
在map中的排序是基于按照key来排序的,所以无法对value直接进行排序,如果想对value进行排序,需要用到vector容器以及sort函数.
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
//map是个键值对,所以需要vector来接收,那么就需要一对一,就需要用到pair(对组)
bool cmp(const pair<string, int> a, pair<string, int> b){
return a.second > b.second; //按照值从大到小排序
}
int main()
{
map <string, int> m;
m["emxs"] = 22;
m["abs"] = 18;
m["cgg"] = 66;
vector < pair<string, int> > v(m.begin(), m.end());
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < v.size(); i++){
cout << v[i].first << " " << v[i].second << endl;
}
return 0;
}
/*
输出:
cgg 66
emxs 22
abs 18
*/