map正序、逆序排序
一、按 key 排序
1.map顺序排序(小的在前,大的在后):
map<float,string,less<float> > m_aSort;//已float从小到大排序
2.map逆序排序(大的在前,小的在后):
map<float,string,greater<float> > m_aSortR;//已float从大到小排序

注:
用 greater_equal(less_equal) 做第三个参数来排序,map的 find 函数会失败。
如果需要存储相同key值的 键值对,可以用multimap,参见:https://www.cnblogs.com/SZxiaochun/p/10482964.html
struct SIndexData
{
string sCode;
int iDate;
int iTime;
float f1;
SIndexData():
iDate(0),
iTime(0)
{
} bool operator == (const SIndexData & obj) const
{
return sCode == obj.sCode && iDate == obj.iDate && iTime == obj.iTime;
} bool operator < (const SIndexData & obj) const
{
return iDate < obj.iDate ;
}
}; map<float, SIndexData, greater_equal<float> > m_is; SIndexData sIndexData1;
sIndexData1.sCode = "000001";
sIndexData1.iDate = 20171025;
sIndexData1.iTime = 94000;
sIndexData1.f1 = 7.8f;
m_is[sIndexData1.f1] = sIndexData1; SIndexData sIndexData2;
sIndexData2.sCode = "000002";
sIndexData2.iDate = 20171025;
sIndexData2.iTime = 94000;
sIndexData2.f1 = 7.9f;
m_is[sIndexData2.f1] = sIndexData2; SIndexData sIndexData3;
sIndexData3.sCode = "000004";
sIndexData3.iDate = 20171025;
sIndexData3.iTime = 94000;
sIndexData3.f1 = 7.8f;
//m_is[sIndexData3.f1] = sIndexData3;
m_is.insert(make_pair(sIndexData3.f1, sIndexData3)); SIndexData sIndexData4 = m_is[sIndexData3.f1]; map<float, SIndexData, greater_equal<float> >::iterator iter = m_is.find(sIndexData3.f1);
if (iter != m_is.end())
{
cout << "find succ" << endl;
}
else
{
cout << "find error" << endl;
} getchar();



原因分析参见:https://www.cnblogs.com/Harley-Quinn/p/7247978.html
二、按 value 排序
typedef pair<int, string> PAIR; bool CmpByValue(const PAIR & lhs, const PAIR & rhs)
{
return lhs.second < rhs.second; //按 value 从小到大排序。如果 value 不是基础类型(int、short等),而是自己定义的结构体,就在结构体中重载 < 运算符(详见:http://www.cnblogs.com/SZxiaochun/p/6377084.html 第41行)。
} map<int,string> m_two; m_two[1] = "xiaochun";
m_two.insert(make_pair(2,"chunxiao")); vector<PAIR> vPair;
vPair.insert(vPair.end(), m_two.begin(), m_two.end()); sort(vPair.begin(), vPair.end(), CmpByValue);
map正序、逆序排序的更多相关文章
- C#数组的排序(正序逆序)
C#数组的排序(正序逆序) 这种排序 超级简单的 ! using System; using System.Collections.Generic; using System.Linq; using ...
- oracle 正序 逆序 排序查询
正序:从小到大 order by t.id asc 逆序:从大到小 order by t.id desc
- c - 逆序/正序输出每位.
#include <stdio.h> #include <math.h> /* 判断一个正整数的位数,并按正序,逆序输出他们的位. */ int invert(int); vo ...
- Comparable 接口学习:对对象List进行比较和排序(正序和逆序)
Comparable 接口只有一个 int compareTo(T o) 方法 1.int compareTo(T o) 方法 方法说明: 比较此对象和规定的对象,如果此对象大于,等于,小于规定对象, ...
- Java之选择排序(正序、逆序)
public class SelectSort { public static void main(String[] args) { /** * @author JadeXu * @// TODO: ...
- C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等
上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm> 函数名 功能描述 sort 对给定区间所有元素进行排序 st ...
- 链表的创建、测长、排序、插入、逆序的实现(C语言)
#include <stdio.h> #define END_elem 0 struct node { int date; struct node * next; }; //链表创建 no ...
- C语言链表全操作(增,删,改,查,逆序,递增排序,递减排序,链式队列,链式栈)
一,数据结构——链表全操作: 链表形式: 其中,每个节点(Node)是一个结构体,这个结构体包含数据域,指针域,数据域用来存放数据,指针域则用来指向下一个节点: 特别说明:对于单链表,每个节点(Nod ...
- 【变态需求】bootstrapTable列排序-选择正序倒序不排序
产品经理:那个table排序能不能点击后弹个选项选择正序倒序不排序? -- 那个是bootstrapTable的插件!不支持!改不了!! 注意:数据上假的,效果看http请求参数进行脑补 这是boot ...
随机推荐
- Python2 字典 cmp() 函数
描述 Python 字典的 cmp() 函数用于比较两个字典元素,如果 dict1 < dict2 返回 -1, 如果 dict1 == dict2 返回 0, 如果 dict1 > di ...
- unity, 人物与移动跳台的交互
----更新(2015-7-1): 也可以用itween来移动跳台. ---- 例如人物跳到往复运动的移动跳台上后应随跳台运动. 要实现这个效果有两个思路,一是用运动学一是用动力学. 用动力学实现就是 ...
- 在为shader program pass uniform之前一定要先use program。
在为shader program pass uniform之前一定要先use program.
- Andriod Studio中setText输出中文在AVD中显示乱码的解决方法
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ...
- 使用SQL语句的子查询批量复制表数据
批量复制表数据这里有两种方法,下面分别来介绍这两种方法: 一.手动创建新表,然后复制数据 如果是要复制整个表的话,可以使用SQL SERVER自动生成CREATE脚本: 然后在脚本中改改表名就可以了, ...
- vim:将<esc>映射为CapsLock键
无语,大写锁定键基本不用,却占据这么重要的位置,凭啥?换了,搜了半天,决定用它uncap(https://github.com/susam/uncap) 一个开放源代码的小工具,就位踢走大写键而生地, ...
- Java并发和多线程那些事儿
我记得我接触电脑的时候是在小学三年级的时候,那是1995年,那年发布了windows95,但是我学习的时候还是只是dos系统,简单对于文件的一些命令操作还有五笔 在过去的那个年代,电脑都是单CPU,也 ...
- C#匿名对象的使用
单个对象 new { Inv = item.Inv, Count = item.Count } 数组 var testData = new[] { }, }, }, }, }, }, }, }, } ...
- Oracle PLSQL Demo - 24.分隔字符串function
-- refer: -- http://www.cnblogs.com/gnielee/archive/2009/09/09/1563154.html -- http://www.cnblogs.co ...
- Zookeeper的Quorum机制-谈谈怎样解决脑裂(split-brain)
在使用zookeeper的过程中,我们经常会看到这样一些说法: 1.zookeeper cluster的节点数目必须是奇数. 2.zookeeper 集群中必须超过半数节点(Majority)可用,整 ...