结构体作为map的key或放入set中,需要重载<运算符
结构体作为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中,需要重载<运算符的更多相关文章
- GO学习-(38) Go语言结构体转map[string]interface{}的若干方法
结构体转map[string]interface{}的若干方法 本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若 ...
- c#---部分;把数组或者结构体存入集合里,然后再从集合中取出之后,输出;foreach既可以用到提取数组重点额数据,也可以提取集合中的数据(前提是集合中的元素是相同数据类型)
1.输入班级人数,统计每个人的姓名,性别,年龄:集合与数组 //Console.Write("请输入班级人数:"); //int a = int.Parse(Console.Rea ...
- tuple放入dict中
tuple放入dict中是否可以正常运行 # 将tuple放入dict中 a = ('AI','Kobe','Yao') b = ('AI',['Kobe','Yao']) dict1 = {'a': ...
- pyqt字符串分离开,放入列表中
string1 = ''''' the stirng Has many line In THE fIle ''' list_of_string = string1.split() print list ...
- set是无序集合,放入set中的元素通过iterator输出时候是无序的
set是无序集合,放入set中的元素通过iterator输出时候是无序的 HashMap<String , String> hashMap = new HashMap<String ...
- java通过文件路径读取该路径下的所有文件并将其放入list中
java通过文件路径读取该路径下的所有文件并将其放入list中 java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...
- 在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可
在查询时将查询条件放入Session中,导出时直接根据qpniRGaFiler取查询条件即可
- 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中
前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...
- .Net中把图片等文件放入DLL中,并在程序中引用
原文:.Net中把图片等文件放入DLL中,并在程序中引用 [摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵 ...
随机推荐
- UML学习之用例图
在UML的整个学习过程中,9种图(用例图.活动图.状态图.顺序图.类图.对象图.协作图.组件图.部署图)的学习以及常用开发.建模工具的使用是最为重要的一个阶段,它是进行UML建模的基础.在本篇文章中首 ...
- Flask项目之手机端租房网站的实战开发(七)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- OpenCV func
cvLoadImage("lena.jpg", CV_LOAD_IMAGE_COLOR); //CV_LOAD_IMAGE_GRAYSCALE //0
- 下拉列表,点击选择实现跳转链接 onchange="window.location=..."
<select onchange="window.location=this.value;"> <option value="a.html"& ...
- Redis学习笔记--Hash(五)
Redis的数据是通过key-value的方式存储的,对于value的数据类型有字符串.Hash.list.set.sortedSet在redis命令语句中,语句是忽略大小写的,但是key是不可以忽略 ...
- Nginx- 实现跨域访问
https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80688740
- radare, the reverse engineering framework
History The radare project [http://radare.org/] started in February of 2006 aiming to provide a free ...
- jquery常用方法总结(转)
取值与赋值操作 $("#ID").val(); //取value值 $("#ID").val("xxx"); //赋值 $("#I ...
- [TypeStyle] Style CSS pseudo elements with TypeStyle
Just like pseudo-classes, pseudo-elements are added to selectors but instead of describing a special ...
- python链表的实现,有注释
class Node(): #node实现,每个node分为两部分:一部分含有链表元素,成数据域;另一部分为指针,指向下一个 __slots__=['_item' ...