结构体作为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 = { 1, 1};
    stMap.insert(pair<Array, TInfo>(Array(1, 2), stInfo1));

TInfo stInfo2 = { 2, 1, 1 };
    stMap.insert(pair<Array, TInfo>(Array(2, 2), stInfo2));

TInfo stInfo3 = { 3, 1, 1 };
    stMap.insert(pair<Array, TInfo>(Array(3, 2), stInfo3));

for (map<Array, TInfo, greater<Array> >::iterator it = stMap.begin(); it != stMap.end(); ++it)
    {
        cout << it->second.m_i32Num1 << endl;
    }

return 0;
}

说明:
 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) < 0;
    }
};

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) == 0;
    }
};

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();
        }
    };
}

结构体作为map的key或放入set中,需要重载<运算符的更多相关文章

  1. GO学习-(38) Go语言结构体转map[string]interface{}的若干方法

    结构体转map[string]interface{}的若干方法 本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若 ...

  2. c#---部分;把数组或者结构体存入集合里,然后再从集合中取出之后,输出;foreach既可以用到提取数组重点额数据,也可以提取集合中的数据(前提是集合中的元素是相同数据类型)

    1.输入班级人数,统计每个人的姓名,性别,年龄:集合与数组 //Console.Write("请输入班级人数:"); //int a = int.Parse(Console.Rea ...

  3. tuple放入dict中

    tuple放入dict中是否可以正常运行 # 将tuple放入dict中 a = ('AI','Kobe','Yao') b = ('AI',['Kobe','Yao']) dict1 = {'a': ...

  4. pyqt字符串分离开,放入列表中

    string1 = ''''' the stirng Has many line In THE fIle ''' list_of_string = string1.split() print list ...

  5. set是无序集合,放入set中的元素通过iterator输出时候是无序的

    set是无序集合,放入set中的元素通过iterator输出时候是无序的 HashMap<String , String> hashMap = new HashMap<String ...

  6. java通过文件路径读取该路径下的所有文件并将其放入list中

    java通过文件路径读取该路径下的所有文件并将其放入list中   java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...

  7. 在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可

    在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可

  8. 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中

     前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...

  9. .Net中把图片等文件放入DLL中,并在程序中引用

    原文:.Net中把图片等文件放入DLL中,并在程序中引用 [摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵 ...

随机推荐

  1. GO语言学习(九)Go 语言运算符

    运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 接下来让我们来详细看看各个运算符的介绍. 算术运算符 下表 ...

  2. Vue实现上传图片功能

    前言: 用vue实现上传图片功能,效果图如下: 先说文件上传控件样式美化怎么做,我有两种方法. 1.先上代码 html部分: <div class="pics-wrapper" ...

  3. 【例题 6-3 UVA - 442】Matrix Chain Multiplication

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用栈来处理一下表达式就好. 因为括号是一定匹配的.所以简单很多. ab x bc会做abc次乘法. [代码] #include< ...

  4. Geodatabase模型

    原文 Geodatabase模型 地理数据模型是地理实体及其关系的形式化抽象和数学描述.随着数据库.面向对象等技术的发展,面向对象的地理数据模型成为大型空间数据库的首选方案,它克服了传统地理数据模型的 ...

  5. [RxJS] ReplaySubject with buffer

    A BehaviorSubject can remember the latest value emitted, but what if we wanted Observer B to see all ...

  6. 部分和(partial sum)在算法求解中的作用

    C++ 的 STL 库的 <numeric> 头文件的 partial_sum 函数已实现了对某一序列的 partial sum. partial_sum(first, last, des ...

  7. CSS垂直居中的实现

    这个问题可以说是老生常谈了,面试时经常问道,一直没整理过,这次做个系统梳理 1.利用display:table实现 从caniuse.com上查到,display:table可以兼容到IE8,以目前环 ...

  8. poj 2063 Investment ( zoj 2224 Investment ) 完全背包

    传送门: POJ:http://poj.org/problem?id=2063 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  9. matlab 程序发布

    将matlab程序发布为可执行程序包 说明,这种可执行程序包可以在没有安装matlab的计算机上运行. 1. 打开Applicaiton Compler 如果下拉列表中没有这个APPLICATIOND ...

  10. 《Java设计模式》之抽象工厂模式

    场景问题 举个生活中常见的样例--组装电脑.我们在组装电脑的时候.通常须要选择一系列的配件,比方CPU.硬盘.内存.主板.电源.机箱等. 为讨论使用简单点.仅仅考虑选择CPU和主板的问题. 其实,在选 ...