c++中map按key和value排序
```
#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排序的更多相关文章
- JAVA中对List<map<String,Object>>根据map某个key值进行排序
方法compareTo()比较此对象与指定对象的顺序.如果该对象小于.等于或大于指定对象,则分别返回负整数.零或正整数.返回整数,1,-1,0:返回1表示大于,返回-1表示小于,返回0表示相等. 普通 ...
- JAVA中对list map根据map某个key值进行排序
package test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; ...
- list中map 的value值时间排序
public static void main(String[] args) { String sys=DateUtil.getTime().substring(0,5); System.out.pr ...
- map的key 为指针
STL中map的key能否用char *呢?当然可以! 在程序中需要用到一个map,本来是这样写的,map<string, int> mapStr; 为了追求效率,把string改成了ch ...
- JS中map list 数组的迭代
后台传给前台一个map 前台如何迭代呢 $.post("getSys.jhtml", function(data){ var temp = ""; $.each ...
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...
- C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET
C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...
- Java中Map根据键值(key)或者值(value)进行排序实现
我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...
- C++ STL中Map的按Key排序跟按Value排序
C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...
随机推荐
- p中不能包含div
一句话:有些块元素不可以包含另一些块元素 ,DTD中规定了块级元素是不能放在P里;P标签内包含块元素时,它会先结束自己,比如:<*p><*div>测试p包含div<*/d ...
- Spark2.3(三十七):Stream join Stream(res文件每天更新一份)
kafka测试数据生成: package com.dx.kafka; import java.util.Properties; import java.util.Random; import org. ...
- .NET Core Entity使用Entity Framework Core链接数据库
首先安装Nuget包 Install-package Microsoft.EntityFrameworkCore Install-package Microsoft.EntityFrameworkCo ...
- C++中 线程函数为静态函数 及 类成员函数作为回调函数
线程函数为静态函数: 线程控制函数和是不是静态函数没关系,静态函数是在构造中分配的地址空间,只有在析构时才释放也就是全局的东西,不管线程是否运行,静态函数的地址是不变的,并不在线程堆栈中static只 ...
- texlive测试是否安装成功
在完成TEX Live安装之后,自然你会希望试试看它是否正常工作,好让你在以后能够创建优美的文档和字体. 1.首先确认你可以执行tex程序: >tex --version TeX 3.14159 ...
- uboot i2c 操作函数记录
I2C 在 u-boot 上面,有直接操作 I2C 的函数 // drivers/i2c/i2c_core.c // 设置在哪个 I2C bus 上工作 276 int i2c_set_bus_num ...
- mysql 修改表的每个列的字符类型
#!/bin/shfor i in $(mysql -uroot -p112358s uarticles_2019 -e "show tables;"|egrep -v Table ...
- Java知多少(31)static关键字以及Java静态变量和静态方法
static 修饰符能够与变量.方法一起使用,表示是“静态”的. 静态变量和静态方法能够通过类名来访问,不需要创建一个类的对象来访问该类的静态成员,所以static修饰的成员又称作类变量和类方法.静态 ...
- Java知多少(54)断言详解
断言的概念 断言用于证明和测试程序的假设,比如“这里的值大于 5”.断言可以在运行时从代码中完全删除,所以对代码的运行速度没有影响. 断言的使用 断言有两种方法: 一种是 assert<< ...
- Json Web Token(JWT)
Json web token (JWT),是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(Si ...