C&C++
发布时间:2022-04-03 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C++的map排序,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一 点睛
@H_776_2@map的排序默认按照key从小到大进行排序,但有以下几点需要注意:
1 按照key从大到小进行排序。
2 key的第1个元素是结构体。
3 想按value(第二个元素)排序。
二 让map中的元素按照key从大到小排序
1 代码
@H_
419_33@
#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;
}
@H_
607_35@
2 运行
@H_
419_33@
[root@localhost charpter03]# g++ 0327.cpp -o 0327
[root@localhost charpter03]# ./0327
ZiLinMi 72
LiMin 90
BoB 79
@H_
607_35@
三 重定义map内部的Compare函数,按照键字符串长度大小进行排序
1 代码
@H_
419_33@
#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;
}
@H_
607_35@
2 运行
@H_
419_33@
[root@localhost charpter03]# g++ 0328.cpp -o 0328
[root@localhost charpter03]# ./0328
BoB 79
LiMin 90
ZiLinMi 72
@H_
607_35@
四 key是结构体的排序
1 代码
@H_
419_33@
#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;
}
@H_
607_35@
2 运行
@H_
419_33@
[root@localhost charpter03]# g++ 0329.cpp -o 0329
[root@localhost charpter03]# ./0329
1 student_one 90
2 student_two 80
@H_
607_35@
五 将map按value排序
1 代码
@H_
419_33@
#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;
}
@H_
607_35@
2 运行
@H_
419_33@
[root@localhost charpter03]# g++ 0330.cpp -o 0330
[root@localhost charpter03]# ./0330
ZiLinMi 79
Albert 86
LiMin 90
BoB 92
Bing 99
@H_
607_35@
大佬总结
以上是大佬教程为你收集整理的C++的map排序全部内容,希望文章能够帮你解决C++的map排序所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: