map重写比较器
结构体作为map的key或放入set中,需要重载<运算符,如下: typedef struct tagRoadKey
{
int m_i32Type;
int m_i32Scale; bool operator <(const tagRoadKey& other) const // 注意是const函数!!
{
if (m_i32Type != other.m_i32Type) // 类型按升序排序
{
return (m_i32Type < other.m_i32Type);
}
else // 如果类型相同,按比例尺升序排序
{
return (m_i32Scale < other.m_i32Scale);
}
} } RoadKey; 也可以重载>运算符,示例如下: #include <iostream>
#include <string>
#include <map> using namespace std; class Array
{
private:
int m_i32Num1;
int m_i32Num2; public:
Array(int i32Num1, int i32Num2);
bool operator >(const Array& other) const;
}; Array::Array(int i32Num1, int i32Num2)
{
m_i32Num1 = i32Num1;
m_i32Num2 = i32Num2;
} bool Array::operator >(const Array& other) const
{
if (m_i32Num1 > other.m_i32Num1)
{
return true;
}
else
{
return false;
}
} // 此结构体作为map的value
struct TInfo
{
int m_i32Num1;
int m_i32Num2;
}; int main(int argc, char* argv[])
{
map<Array, TInfo, greater<Array> > stMap; TInfo stInfo1 = { , };
stMap.insert(pair<Array, TInfo>(Array(, ), stInfo1)); TInfo stInfo2 = { , , };
stMap.insert(pair<Array, TInfo>(Array(, ), stInfo2)); TInfo stInfo3 = { , , };
stMap.insert(pair<Array, TInfo>(Array(, ), stInfo3)); for (map<Array, TInfo, greater<Array> >::iterator it = stMap.begin(); it != stMap.end(); ++it)
{
cout << it->second.m_i32Num1 << endl;
} return ;
}
说明:
map缺省是用less<Key>作为比较器,所以它要求作为Key的类要重载“<”操作符,没有重载“<”操作符,而是重载了“>”操作符就会报错。
反之,也可以显式地用greater<Key>作为比较器,此时就必要重载Key类中的“>”操作符了。
附:stl中map和set的声明,二者比较像,底层都是用红黑树实现的 template < class Key, class Compare = less<Key>,
class Allocator = allocator<Key> > class set; template < class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > > class map; template < class Key, class Compare = less<Key>,
class Allocator = allocator<Key> > class multiset; template < class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > > class multimap; 从上面的声明可以看出,也可以定义一个函数对象Compare,声明map或set类型时传进入,如: struct TTimeCompare
{
bool operator ()(const CTimerEvent* po1, const CTimerEvent* po2)const
{
return (po1->m_oNextTick < po2->m_oNextTick);
}
}; typedef multiset<CTimerEvent*, TTimeCompare> TEventSet; struct ltstr // less than
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < ;
}
}; set<const char*, ltstr> stSet; // set<Key, Compare, Alloc>
map<const char*, int, ltstr> stMap; // map<Key, Data, Compare, Alloc> struct eqstr // equal
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == ;
}
}; hash_map<const char*, int, hash<const char*>, eqstr> stHashMap; // hash_map<Key, Data, HashFcn, EqualKey, Alloc> // 自定义hash函数
namespace std
{
template<>
struct hash<KEY_TYPE>
{
size_t operator()(const KEY_TYPE& key) const
{
//return key.Hash();
}
};
}
相等的时候返回false,否则报错
map重写比较器的更多相关文章
- 中招了,重写TreeMap的比较器引发的问题…
需求背景 给一个无序的map,按照value的值进行排序,value值越小,排在越前面. key和value都不为null value可能相同 返回结果为一个相同的有序map 代码如下所示: 1 // ...
- C++ map 映照容器
map映照容器的元素数据是一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系. map映照容器的数据结构是采用红黑树来实现的,插入键值的元素不允许重复,比较函数只对元素的键值进行比较, ...
- Map集合——双列集合
双列集合<k, v> Map: Map 和 HashMap是无序的: LinkedHashMap是有序的: HashMap & LinkedHashMap: put方法: 其中,可 ...
- Map以及其子类
package com.Map; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; i ...
- 重写Euqals & HashCode
package com.test.collection; import java.util.HashMap; import java.util.Map; /** * 重写equals & ha ...
- java中sort方法的自定义比较器写法(转载)
java中sort方法的自定义比较器写法 摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collecti ...
- sort方法和自定义比较器的写法
摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...
- MapReduce 常见SQL模型解析
MapReduce应用场景 前一阵子参加炼数成金的MapReduce培训,培训中的作业例子比较有代表性,用于解释问题再好不过了.有一本国外的有关MR的教材,比较实用,点此下载. MR能解决什么问题?一 ...
- Java基础学习(四)-- 接口、集合框架、Collection、泛型详解
接口 一.接口的基本概念 关键字为:Interface,在JAVA编程语言中是一个抽象类型,是抽象方法的集合.也是使用.java文件编写. 二.接口声明 命名规范:与类名的命名规范相同,通常情况下 ...
随机推荐
- Dbvisual连接远程数据库报错Error Code: 17401
Long Message:违反协议 Details: Type: java.sql.SQLException Error Code: 17401 SQL State: null 现象: 本 ...
- 关于 AngularJS 的数据绑定
单向绑定(ng-bind) 和 双向绑定(ng-model) 的区别 ng-bind 单向数据绑定($scope -> view),用于数据显示,简写形式是 {{}}. 1 <span n ...
- HDU 1016:Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- vim的窗口切换
当用vim写代码的时候,我喜欢一边看着头文件中结构的定义,一边编写实现的代码,这样就经常用到多窗口来编辑,查看文档. 1.同时打开多个文件,并横向排列 vim -o t.c t.h 2.同时打开多个文 ...
- atitit.自己动手开发编译器and解释器(1) ------词法分析--attilax总结
atitit.自己动手开发编译器and解释器(1) ------词法分析--attilax总结 1. 应用场景:::DSL 大大提升开发效率 1 2. 2. 流程如下::: 词法分析(生成toke ...
- Spring学习10-SpringMVC入门
二.SpringMVC请求处理流程 其中Front controller :前端控制器 Controller:后端控制器 三.Spring核心组件及请求处理流程
- django model 多对多保存
- jquery 取第一个兄弟节点
1.HTML <table> <tr> <td>1</td> <td>abc</td> <td>def</td ...
- IIS 与 Apache共存
http://support.microsoft.com/kb/813368 http://www.blueidea.com/computer/system/2006/3585.asp 操作其实也很简 ...
- C# Dictionary 复制
Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add(" ...