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文件编写. 二.接口声明 命名规范:与类名的命名规范相同,通常情况下 ...
随机推荐
- 摘:数据结构各种算法实现(C++模板)
目 录 1.顺序表. 1 Seqlist.h 1 Test.cpp 6 2.单链表. 8 ListNode.h 8 SingleList.h 10 test.cpp 20 3.双向链表. 22 No ...
- jQuery knowledge
I have used jquery for many years, but didn't list the problem I ever meeting, so here is a list of ...
- StrongLoop
http://loopback.io/getting-started/ 使用 StrongLoop 创建 Node.js MySQL 应用程序 StrongLoop 是 IBM 的一家子公司,Stro ...
- Linux 命令之权限修改
chmod 改变一个文件的权限:chmod [mode] file.txt改变一个目录的权限:chmod [mode] dir改变一个目录和其子目录的权限: chmod [mode] dir - ...
- 回文串dp
一个字符串如果从左往右读和从右往左读都一样,那么这个字符串是一个回文串.例如:"abcba","abccba". 蒜头君想通过添加字符把一个非回文字符串变成回文 ...
- javac编译出来的程序运行报错“错误: 找不到或无法加载主类”
使用javac编译java文件生成class文件 >javac HelloWorld.java执行class文件>java HelloWorld 原因: 含有包名 解决办法: 按照包的结构 ...
- hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)
刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...
- java常用操作
1.properties文件中文转换 在cmd中进入到文件所在目录执行(其他操作请见命令帮助):native2ascii -reverse messages_zh_CN.properties b.t ...
- sama5d36 can0 can1 测试
1 删除/bin/ip 保留/sbin/ip 2 ip link set can0 type can bitrate 125000 ip link set can1 type can bitrate ...
- JDK1.8与spring3.x的不兼容
今天运气很好,两次遇到了这个兼容性问题,spring3.x不支持 java 1.8 byte code format!! 九月 10, 2017 7:17:18 上午 org.apache.catal ...