struct作为map的key时,需要重载该结构体
来源:互联网 发布:mysql删除sql语句 编辑:程序博客网 时间:2024/05/21 11:27
当结构体作为map中的key时,这个结构体必须重载"<"运算符, 否则将出错,看我完整代码:
#ifndef WIN32#include <string.h>#else#include <string>#endif#include <iostream>#include <map>using namespace std;typedef struct KeyNode_s{long key;bool operator < (const KeyNode_s& s) const{return key < s.key;}}KeyNode;typedef struct N_1{int c;}CRTPSession;typedef struct N_2{int d;}CJOpPlayback;typedef struct SessionInfo_s{CRTPSession*_session;CJOpPlayback*_opPlayback;SessionInfo_s(){_session = NULL;_opPlayback = NULL;}}SessionInfo;typedef struct Hello_s{char name[20];Hello_s(const char* s){strcpy(name, s);}bool operator < (const Hello_s& s) const{return strcmp(name, s.name) < 0;}}Hello;typedef std::pair<KeyNode, Hello> GBClientPair;typedef std::map<GBClientPair, SessionInfo> RtpSessionList;// map中的key是pair, 但是pair中有结构体, 所以这些结构体必须重载"<"运算符void test(){KeyNode knode;SessionInfo sinfo;Hello h("world");RtpSessionList list;GBClientPair pa(knode, h);list[pa] = sinfo;}int main(){test();return 0;}
1 0
- struct作为map的key时,需要重载该结构体
- C++结构体作为map的key的时候需要重载<运算符
- 结构体作为map的key或放入set中,需要重载<运算符
- 结构体作为map的key或放入set中,需要重载<运算符
- struct作为map的key
- 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)
- 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)
- 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)已跪~~~~(>_<)~~~~
- 【C++】自定义结构体作为map的key
- map结构体作为key值的使用
- map的key元素为结构体时该结构体需重截操作符<
- 自定义结构作为map的key,比较函数写法
- C++类或结构作为map的key值
- 自定义结构作为map的key,比较函数写法
- Range作为map的Key
- Map的key为自定义结构体
- groovy 使用变量值作为 map 的key--需要注意的
- 为实现非系统类作为Map的key需要覆写hashCode()和equals()方法
- 数据库中substring的使用方法 CONVERT(varchar(12) , getdate(), 112 )
- JSP知识3
- 爱情就是一物降一物——金庸教你谈恋爱
- mysql学习笔记
- 关于mybatis 中collection does not contain value for
- struct作为map的key时,需要重载该结构体
- Sublime Text2/3怎样在Ubuntu中配置CTags插件
- 关于webservice c调用
- OpenERP工作流浅析
- onnewintent的使用时机
- 思考
- iOS中属性与成员变量的区别
- DMA基本概念及linux2440下DMA驱动程序编写与测试
- JSP知识4