```
#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. 快速准备(复制替换)一套新测试环境,CentOS7 MySQL相关配置

    拿到一个新环境,需要找相关配置,我有一个办法,相对能比较快速地复制一套环境出来. 修改机器配置: virsh 相关几条命令,已完成,后续我再整理补充... 虚拟化相关,参考:https://www.c ...

  2. 屌丝也用按位与(&),按位或(|) (二)

    上篇讲到了,用按位操作进行配置,下边来讲一个大神也在用的例子 linux的权限系统 我们知道一个文件,如果是777,那么是最高的权限,那这个七是咋构成的,我会告诉你是 4+2+1么? 我会告诉你,是1 ...

  3. Java 基础【17】 异常与自定义异常

    1.异常的分类 Throwable 是所有异常类的基类,它包括两个子类:Exception 和 Error. a. 错误 (Error) 错误是无法难通过程序来解决的,所以程序不应该抛出这种类型的对象 ...

  4. dyld`__abort_with_payload:

    dyld`__abort_with_payload: 0x1030422f0 <+0>:  mov    x16, #0x209 0x1030422f4 <+4>:  svc  ...

  5. CentOS7下 让Docker pull命令使用squid做http代理拉取目标镜像仓库的镜像

    场景,如下图所示: 服务器B具有两个网卡,分别和服务器A和服务器C互通,这里想要在服务器C上借助服务器B作为桥梁,拉取镜像仓库服务器A上的镜像. 思路也很简单,在服务器上搭建HTTP代理服务,服务器C ...

  6. 内核编译之vmlinuz vmlinux system.map initrd

    一.vmlinuz  vmlinuz是可引导的.压缩的内核.“vm”代表“Virtual Memory”.Linux 支持虚拟内存,不像老的操作系统比如DOS有640KB内存的限制.Linux能够使用 ...

  7. C#:网络传输问题

    1.Http Post Header 中文数据值,服务端接收Header 中文数据值乱码问题: 客户端:    Encoding utf8Encoding = Encoding.GetEncoding ...

  8. 开发FTP不要使用sun.net.ftp.ftpClient

    转自:http://cai21cn.iteye.com/blog/700188 在开发一个web应用过程中,需要开发一个服务使用ftp功能将数据传输一个网外的ftp服务器.最初使用sun.net.ft ...

  9. 每日英语:Upgrade Your Life: How to speed up your PC (or Mac)

    Is your desktop or laptop computer starting to feel a little poky? Even after just a few months of u ...

  10. SAP BW: Replacement Path Variables

    How to use Replacement Path Variables to perform Date Calculations A Step-by-Step guide Have you eve ...