```
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
using namespace std; /*按key升序*/
void test01(){
map<string,int,less<string> > map1;
map1.insert(pair<string,int>("aba",));
map1.insert(pair<string,int>("aaa",));
map1.insert(pair<string,int>("ddd",));
map1.insert(pair<string,int>("ccc",)); for(map<string,int>::iterator it=map1.begin(); it!=map1.end(); it++ ){
cout<<(*it).first<<" : "<<(*it).second<<endl;
}
}
/*按key降序*/
void test02(){
map<string,int,greater<string> > map1;
map1.insert(pair<string,int>("aba",));
map1.insert(pair<string,int>("aaa",));
map1.insert(pair<string,int>("ddd",));
map1.insert(pair<string,int>("ccc",)); for(map<string,int>::iterator it=map1.begin(); it!=map1.end(); it++ ){
cout<<(*it).first<<" : "<<(*it).second<<endl;
}
}
/*----------------------------------------------------*/
/*编写类或者结构体按key升序或者降序*/
class flag{
public:
bool operator()(string v1,string v2){
return v1<v2;
}
}; void test03(){
map<string,int,flag> map1;
map1.insert(pair<string,int>("aba",));
map1.insert(pair<string,int>("aaa",));
map1.insert(pair<string,int>("ddd",));
map1.insert(pair<string,int>("ccc",)); for(map<string,int>::iterator it=map1.begin(); it!=map1.end(); it++ ){
cout<<(*it).first<<" : "<<(*it).second<<endl;
}
}
/*-------------------------------------------------------*/
/*自定义编写类或者函数或者结构体进行值排序*/
/*自定义函数编写不用(),自定义类或者结构体需要()*/
bool flag_2(pair<string,int> o1,pair<string,int> o2){
return o1.second>o2.second;
}
class flag_2{
public:
bool operator()(pair<string,int> o1,pair<string,int> o2){
return o1.second>o2.second;
}
};
struct flag_2{
bool operator()(pair<string,int> o1,pair<string,int> o2){
return o1.second<o2.second;
}
};
void test04(){
map<string,int> map1;
map1.insert(pair<string,int>("aba",));
map1.insert(pair<string,int>("aaa",));
map1.insert(pair<string,int>("ddd",));
map1.insert(pair<string,int>("ccc",)); //利用vector进行value排序
vector< pair<string,int> > dic1(map1.begin(),map1.end());
sort(dic1.begin(),dic1.end(),flag_2()); for(int i=; i<dic1.size(); i++ ){
cout<<dic1[i].first<<" "<<dic1[i].second<<endl;
}
}
int main(){
//test01();
//test02();
//test03();
test04(); return ;
} ```

c++中map按key和value排序的更多相关文章

  1. JAVA中对List<map<String,Object>>根据map某个key值进行排序

    方法compareTo()比较此对象与指定对象的顺序.如果该对象小于.等于或大于指定对象,则分别返回负整数.零或正整数.返回整数,1,-1,0:返回1表示大于,返回-1表示小于,返回0表示相等. 普通 ...

  2. JAVA中对list map根据map某个key值进行排序

    package test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; ...

  3. list中map 的value值时间排序

    public static void main(String[] args) { String sys=DateUtil.getTime().substring(0,5); System.out.pr ...

  4. map的key 为指针

    STL中map的key能否用char *呢?当然可以! 在程序中需要用到一个map,本来是这样写的,map<string, int> mapStr; 为了追求效率,把string改成了ch ...

  5. JS中map list 数组的迭代

    后台传给前台一个map 前台如何迭代呢 $.post("getSys.jhtml", function(data){ var temp = ""; $.each ...

  6. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

  7. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  8. Java中Map根据键值(key)或者值(value)进行排序实现

    我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...

  9. C++ STL中Map的按Key排序跟按Value排序

    C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...

随机推荐

  1. Spark机器学习(6):决策树算法

    1. 决策树基本知识 决策树就是通过一系列规则对数据进行分类的一种算法,可以分为分类树和回归树两类,分类树处理离散变量的,回归树是处理连续变量. 样本一般都有很多个特征,有的特征对分类起很大的作用,有 ...

  2. JDK自带JVM性能调优监控工具jps、jstack、jmap、jhat、jstat

    原文地址:https://www.jianshu.com/p/db954cb968fb JVM性能调优监控工具jps.jstack.jmap.jhat.jstat位于JDK的bin目录,这些工具短小精 ...

  3. Easyui的DataGrid 清除所有勾选的行。

    $('#grid').datagrid('clearChecked')='none';//清除所有勾选的行.

  4. 【Spark 深入学习-08】说说Spark分区原理及优化方法

    本节内容 ------------------ · Spark为什么要分区 · Spark分区原则及方法 · Spark分区案例 · 参考资料 ------------------ 一.Spark为什 ...

  5. 【Big Data - Hadoop - MapReduce】初学Hadoop之图解MapReduce与WordCount示例分析

    Hadoop的框架最核心的设计就是:HDFS和MapReduce.HDFS为海量的数据提供了存储,MapReduce则为海量的数据提供了计算. HDFS是Google File System(GFS) ...

  6. linux每日命令(26):Linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...

  7. layui表单验证

    layui表单元素的校验只需在元素上加入lay-verify,layui提供了以下值. required(必填项) phone(手机号) email(邮箱) url(网址) number(数字) da ...

  8. Python3自定义http/https请求拦截mitmproxy脚本

    [本文出自天外归云的博客园] 脚本内容 代码如下: from mitmproxy import http, ctx from multiprocessing import Lock class Fil ...

  9. 【Unity】不能新建项目

    问题:Unity5.5.2f1今天遇到个Bug,在启动器点击新建项目没有反应. 办法:先点击新建项目(没有反应),再点击Sign Out退出登录,然后再登录进来,就能跳到新建项目页面.

  10. 创建shell脚本

    1.写一个脚本 a) 用touch命令创建一个文件:touch my_script b) 用vim编辑器打开my_script文件:vi my_script c) 用vim编辑器编辑my_script ...