结构体作为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重写比较器的更多相关文章

  1. 中招了,重写TreeMap的比较器引发的问题…

    需求背景 给一个无序的map,按照value的值进行排序,value值越小,排在越前面. key和value都不为null value可能相同 返回结果为一个相同的有序map 代码如下所示: 1 // ...

  2. C++ map 映照容器

    map映照容器的元素数据是一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系. map映照容器的数据结构是采用红黑树来实现的,插入键值的元素不允许重复,比较函数只对元素的键值进行比较, ...

  3. Map集合——双列集合

    双列集合<k, v> Map: Map 和 HashMap是无序的: LinkedHashMap是有序的: HashMap & LinkedHashMap: put方法: 其中,可 ...

  4. Map以及其子类

    package com.Map; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; i ...

  5. 重写Euqals & HashCode

    package com.test.collection; import java.util.HashMap; import java.util.Map; /** * 重写equals & ha ...

  6. java中sort方法的自定义比较器写法(转载)

    java中sort方法的自定义比较器写法 摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collecti ...

  7. sort方法和自定义比较器的写法

    摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...

  8. MapReduce 常见SQL模型解析

    MapReduce应用场景 前一阵子参加炼数成金的MapReduce培训,培训中的作业例子比较有代表性,用于解释问题再好不过了.有一本国外的有关MR的教材,比较实用,点此下载. MR能解决什么问题?一 ...

  9. Java基础学习(四)-- 接口、集合框架、Collection、泛型详解

    接口 一.接口的基本概念 关键字为:Interface,在JAVA编程语言中是一个抽象类型,是抽象方法的集合.也是使用.java文件编写.   二.接口声明 命名规范:与类名的命名规范相同,通常情况下 ...

随机推荐

  1. C#: 数字经纬度和度分秒经纬度间的转换

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  2. Java Socket网络编程Client端详解

    此类实现客户端套接字(也可以就叫“套接字”).套接字是两台机器之间的通信端点. Socket client = new Socket(ip,port);//创建一个流套接字并将其连接到指定 IP 地址 ...

  3. WiX and System Folders 系统目录 installshield 如何将文件安装到C盘根目录

    Property name Brief description of property AdminToolsFolder Full path to the directory containing a ...

  4. Redis(十五):哨兵Sentinel

    Redis哨兵 Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断地检查你 ...

  5. PL/SQL developer连接oracle出现“ORA-12154:TNS:could not resolve the connect identifier specified”问题的解决

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/50728536 本文出自[我是干勾鱼的博客] 使用PL/SQL developer ...

  6. zookeeper单节点windows下安装

    由于需要在windows下面安装zookeeper,故做个整理 1.下载zookeeper http://mirrors.hust.edu.cn/apache/zookeeper/ 2.解压 3.修改 ...

  7. 如何使用微信JS-SDK实际分享功能

    http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=7

  8. http_load使用详解

    1.什么是http_loadhttp_load是一款基于Linux平台的web服务器性能测试工具,用于测试web服务器的吞吐量与负载,web页面的性能. 2.http_load的安装1)下载地址wge ...

  9. layoutSubviews什么时候触发调用

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ——————- - (void)layoutSubview ...

  10. error: invalid use of incomplete type

    一般出现这种情况都是没有将用到的头文件包含进来 我的情况是在头文件中定义了一个QMenu的指针,在源文件中使用menuBar()函数来返回一个menu指针.我在源文件中包含了文件<QtGui&g ...