结构体作为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. 目标识别(object detection)中的 IoU(Intersection over Union)

    首先直观上来看 IoU 的计算公式: 由上述图示可知,IoU 的计算综合考虑了交集和并集,如何使得 IoU 最大,需要满足,更大的重叠区域,更小的不重叠的区域. 两个矩形窗格分别表示: 左上点.右下点 ...

  2. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  3. iOS_02_第一个C语言程序(理解编译、连接、运行)

    一.开发工具的选择 1. 可以用来写代码的工具:记事本.ULtraEdit.Vim.Xcode等. 2. 选择XCode的原因:苹果公司官方提供的开发利器.简化开发的工程.有高亮显示功能. 3. 使用 ...

  4. spring boot 2.x Path with "WEB-INF" or "META-INF"

    学习spring boot 2.x时,使用jsp作为前端页面.在application.properties配置了jsp所在位置 spring.mvc.view.prefix:/WEB-INF/vie ...

  5. 8.6 Android灯光系统_源码分析_背光灯

    Change system screen brightness, using android.provider.Settings.System.SCREEN_BRIGHTNESSandroid-er. ...

  6. nodejs+express4.0+mongodb安装方法 for Linux, Mac

    废话不多说 1:下载nodejs包 下载地址例如以下:http://www.nodejs.org/download/ 下载source code版本号须要解压后到其文件夹运行./configure,然 ...

  7. 修改SVN中文件的可执行属性

    博文来自下面路径,转载请注明原出处: http://bigwhite.blogbus.com/logs/74568031.html 修改SVN中文件的可执行属性 - [开源世界] Tag:开源世界 S ...

  8. opencv播放不了AVI视频的问题

    有些avi视频的编码可能不是Cinepak Codec by Radius编码格式的,需要转换成这种格式. 我用的是swf转avi视频,在转变换时----->设置---->AVI视频设置- ...

  9. Voronoi Diagram——维诺图

    Voronoi图定义   任意两点p 和q 之间的欧氏距离,记作 dist(p, q) .就平面情况而言,我们有           dist(p, q) =  (px-qx)2+ (py-qy)2 ...

  10. ng-cli搭建angular项目框架

    原文地址 https://www.jianshu.com/p/0a8f4b0f29b3 环境准备 以下步骤都不需要事先创建文件夹,只是环境的准备过程,只有到需要搭建项目的时候才需要创建文件夹用来存放项 ...