0%

std::map 排序相关

对std::map的排序机制进行简单的记录

1.根据key值进行排序

    map默认按照key进行升序排序,和输入的顺序无关。
    如果是int/double等数值型为key,那么就按照大小排列;
    如果是string类型,那么就按照字符串的字典序进行排列

字典序:

    对于两个字符串,大小关系取决于两个字符串从左到右第一个不同字符的ASCII值的大小关系。
比如ah1x小于ahb,而Z5小于a3。下列字符串就是按照字典序进行排列的:

Example 1: 默认按照key升序排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;

int main()
{
map<string, int> scoreMap;
map<string, int>::iterator iter;

scoreMap["LiMin"] = 90;
scoreMap["ZZihsf"] = 95;
scoreMap["Kim"] = 100;
scoreMap.insert(map<string, int>::value_type("Jack", 88));

for(iter=scoreMap.begin(); iter!=scoreMap.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;

return 0;
}

2.按照key值降序排序

map有四个参数,其中第三个参数用于排序用。

equal_to 相等;not_equal_to 不相等;less 小于;greater 大于;less_equal 小于等于;greater_equal 大于等于。

Example 2: 按照key降序排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;

int main()
{
//注意这里的greater<string>,其有个参数就是key的类型,比如这里就是string
map<string, int, greater<string> > scoreMap;
map<string, int, greater<string> >::iterator iter;

scoreMap["LiMin"] = 90;
scoreMap["ZZihsf"] = 95;
scoreMap["Kim"] = 100;
scoreMap.insert(map<string, int>::value_type("Jack", 88));

for(iter=scoreMap.begin(); iter!=scoreMap.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;

return 0;
}

3.自定义规则排序

    以下为按照key中string的长度排序样例;

Example 3: 自定义比较规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;

struct cmp //自定义比较规则
{
bool operator() (const string& str1, const string& str2)
{
return str1.length() < str2.length();
}
};

int main()
{
map<string, int, cmp > scoreMap; //这边调用cmp
map<string, int, cmp >::iterator iter;

scoreMap["LiMin"] = 90;
scoreMap["ZZihsf"] = 95;
scoreMap["Kim"] = 100;
scoreMap.insert(map<string, int>::value_type("Jack", 88));

for(iter=scoreMap.begin(); iter!=scoreMap.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;

return 0;
}

4.根据value值排序

1
2
3
map本身的排序机制无法根据value值排序,实现按照value值排序可以通过把map元素放入vector中,用vector的sort进行排序。

以下是根据map的second值数值大小进行升序排序的样例。

Example 4: 根据value值排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

typedef pair<string, int> PAIR;

struct cmp //自定义比较规则
{
bool operator() (const PAIR& P1, const PAIR& P2) //注意是PAIR类型,需要.firt和.second。这个和map类似
{
return P1.second < P2.second;
}
};


int main()
{
map<string, int> scoreMap; //这边调用cmp
map<string, int>::iterator iter;

scoreMap["LiMin"] = 90;
scoreMap["ZZihsf"] = 95;
scoreMap["Kim"] = 100;
scoreMap.insert(map<string, int>::value_type("Jack", 88));

vector<PAIR>scoreVector;
for(iter=scoreMap.begin(); iter!=scoreMap.end();iter++) //这边本来是使用vector直接初始化的,当时由于vc 6.0 编译器问题,只能这样写,而且还有非法内存。。
scoreVector.push_back(*iter);
//转化为PAIR的vector
sort(scoreVector.begin(), scoreVector.end(), cmp()); //需要指定cmp

for(int i=0; i<=scoreVector.size(); i++) //也要按照vector的形式输出
cout<< scoreVector[i].first<<' '<<scoreVector[i].second <<endl;

/*
for(iter=scoreMap.begin(); iter!=scoreMap.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
*/

return 0;
}