C++的map排序
发布时间:2020-12-16 09:08:34 所属栏目:百科 来源:网络整理
导读:一?点睛 map的排序默认按照key从小到大进行排序,但有以下几点需要注意: 1?按照key从大到小进行排序。 2?key的第1个元素是结构体。 3?想按value(第二个元素)排序。 二?让map中的元素按照key从大到小排序 1?代码 #include map#include string#include iost
一?点睛 map的排序默认按照key从小到大进行排序,但有以下几点需要注意: 1?按照key从大到小进行排序。 2?key的第1个元素是结构体。 3?想按value(第二个元素)排序。 二?让map中的元素按照key从大到小排序 1?代码 #include <map>
#include <string>
#include <iostream>
using namespace std;
int main(){
map<string,int,greater<string> > mapStudent; //关键是这句话
mapStudent["LiMin"]=90;
mapStudent["ZiLinMi"]=72;
mapStudent["BoB"]=79;
map<string,int>::iterator iter=mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
2?运行 [root@localhost charpter03]# g++ 0327.cpp -o 0327
[root@localhost charpter03]# ./0327
ZiLinMi 72
LiMin 90
BoB 79
三?重定义map内部的Compare函数,按照键字符串长度大小进行排序 1?代码 #include <map>
#include <string>
#include <iostream>
using namespace std;
// 自己编写的Compare,实现按照字符串长度进行排序
struct CmpByKeyLength {
bool operator()(const string& k1,const string& k2) {
return k1.length() < k2.length();
}
};
int main(){
map<string,CmpByKeyLength > mapStudent; //这里注意要换成自己定义的compare
mapStudent["LiMin"]=90;
mapStudent["ZiLinMi"]=72;
mapStudent["BoB"]=79;
map<string,int>::iterator iter=mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++){
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
2?运行 [root@localhost charpter03]# g++ 0328.cpp -o 0328
[root@localhost charpter03]# ./0328
BoB 79
LiMin 90
ZiLinMi 72
四?key是结构体的排序 1?代码 #include <map>
#include <string>
#include <iostream>
using namespace std;
typedef struct tagStudentInfo
{
int iID;
string strName;
bool operator < (tagStudentInfo const& r) const {
//这个函数指定排序策略,按iID排序,如果iID相等的话,按strName排序
if(iID < r.iID) return true;
if(iID == r.iID) return strName.compare(r.strName) < 0;
return false;
}
}StudentInfo;//学生信息
int main(){
/*用学生信息映射分数*/
map<StudentInfo,int>mapStudent;
StudentInfo studentInfo;
studentInfo.iID = 1;
studentInfo.strName = "student_one";
mapStudent[studentInfo]=90;
studentInfo.iID = 2;
studentInfo.strName = "student_two";
mapStudent[studentInfo]=80;
map<StudentInfo,int>::iterator iter=mapStudent.begin();
for(;iter!=mapStudent.end();iter++){
cout<<iter->first.iID<<" "<<iter->first.strName<<" "<<iter->second<<endl;
}
return 0;
}
2?运行? [root@localhost charpter03]# g++ 0329.cpp -o 0329
[root@localhost charpter03]# ./0329
1 student_one 90
2 student_two 80
五?将map按value排序 1?代码? #include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef pair<string,int> PAIR;
bool cmp_by_value(const PAIR& lhs,const PAIR& rhs) {
return lhs.second < rhs.second;
}
struct CmpByValue {
bool operator()(const PAIR& lhs,const PAIR& rhs) {
return lhs.second < rhs.second;
}
};
int main(){
map<string,int> name_score_map;
name_score_map["LiMin"] = 90;
name_score_map["ZiLinMi"] = 79;
name_score_map["BoB"] = 92;
name_score_map.insert(make_pair("Bing",99));
name_score_map.insert(make_pair("Albert",86));
/*把map中元素转存到vector中*/
vector<PAIR> name_score_vec(name_score_map.begin(),name_score_map.end());
sort(name_score_vec.begin(),name_score_vec.end(),CmpByValue());
/*sort(name_score_vec.begin(),cmp_by_value);也是可以的*/
for (int i = 0; i != name_score_vec.size(); ++i) {
cout<<name_score_vec[i].first<<" "<<name_score_vec[i].second<<endl;
}
return 0;
}
2?运行 [root@localhost charpter03]# g++ 0330.cpp -o 0330
[root@localhost charpter03]# ./0330
ZiLinMi 79
Albert 86
LiMin 90
BoB 92
Bing 99
? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |