[C/C++]map自定义比较函数
来源:互联网 发布:九宫格图片软件 编辑:程序博客网 时间:2024/06/05 01:17
源地址
在C++中用到map时,如果KEY是自定义的struct,那么需要自己定义比较函数。因为只有基本类型有默认的比较方法。
定义的方法有两种,一是在作为key的struct中,重载操作符less(<),二是自定义仿函数作为map的比较函数,个人比较喜欢第二种方法。
//自定义map的keytypedef struct UrlKey{ uint64_t dwBussID; uint64_t dwVersion; uint64_t dwHashUrl;}UrlKey; //自定义map的valuetypedef struct UrlValue{ string strUrl;}UrlValue; //map的比较函数struct cmp_key{ bool operator()(const UrlKey &k1, const UrlKey &k2)const { if(k1.dwBussID != k2.dwBussID) { return k1.dwBussID < k2.dwBussID; } if(k1.dwVersion != k2.dwVersion) { return k1.dwVersion < k2.dwVersion; } if(k1.dwHashUrl != k2.dwHashUrl) { return k1.dwHashUrl < k2.dwHashUrl; } return false; }}; int main(){ map<UrlKey, UrlValue, cmp_key> UrlMap; UrlKey stKey; stKey.dwBussID = 1; stKey.dwVersion = 2; stKey.dwHashUrl = 3; UrlValue stValue; stValue.strUrl = "abc"; UrlMap[stKey] = stValue; return 0;}
0 0
- [C/C++]map自定义比较函数
- [C/C++]map自定义比较函数
- map自定义比较函数
- map自定义比较函数
- map自定义比较函数
- map自定义比较函数
- map中自定义比较函数
- c++&&map函数
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【C/C++】计时函数比较
- 【kaldi】Kaldi+CUDA安装向问题(含libtool is not installed.的解决方法)
- 2016/6/6 MAYA HELP
- SVN使用教程。
- NoSQL之Redis---SortSet类型命令
- Hiredis_API说明
- [C/C++]map自定义比较函数
- 在ubuntu上搭建GitLab本地服务器
- PB里面播放音乐
- ios网络请求报错:The resource could not be loaded because the App Transport Security policy requires the us
- Erlang初学:Erlang的一些特点和个人理解总结
- layout中引用后定义的控件id不报错?
- HTTP网络请求过程几点说明
- mysql大于等于号
- typeOf与 instanceof的区别