下面是map定义的结构:

  1. // TEMPLATE CLASS map
  2. template<class _Kty,
  3. class _Ty,
  4. class _Pr = less<_Kty>,
  5. class _Alloc = allocator<pair<const _Kty, _Ty> > >
  6. class map
  7. : public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> >
  8. { // ordered red-black tree of {key, mapped} values, unique keys
  9. public:
  10. typedef map<_Kty, _Ty, _Pr, _Alloc> _Myt;
  11. typedef _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> > _Mybase;
  12. typedef _Kty key_type;
  13. typedef _Ty mapped_type;
  14. typedef _Ty referent_type; // retained
  15. typedef _Pr key_compare;
  16. typedef typename _Mybase::value_compare value_compare;
  17. typedef typename _Mybase::allocator_type allocator_type;
  18. typedef typename _Mybase::size_type size_type;
  19. typedef typename _Mybase::difference_type difference_type;
  20. typedef typename _Mybase::pointer pointer;
  21. typedef typename _Mybase::const_pointer const_pointer;
  22. typedef typename _Mybase::reference reference;
  23. typedef typename _Mybase::const_reference const_reference;
  24. typedef typename _Mybase::iterator iterator;
  25. typedef typename _Mybase::const_iterator const_iterator;
  26. typedef typename _Mybase::reverse_iterator reverse_iterator;
  27. typedef typename _Mybase::const_reverse_iterator
  28. const_reverse_iterator;
  29. typedef typename _Mybase::value_type value_type;
  30.  
  31. map()
  32. : _Mybase(key_compare(), allocator_type())
  33. { // construct empty map from defaults
  34. }
  35.  
  36. explicit map(const key_compare& _Pred)
  37. : _Mybase(_Pred, allocator_type())
  38. { // construct empty map from comparator
  39. }
  40.  
  41. map(const key_compare& _Pred, const allocator_type& _Al)
  42. : _Mybase(_Pred, _Al)
  43. { // construct empty map from comparator and allocator
  44. }
  45.  
  46. template<class _Iter>
  47. map(_Iter _First, _Iter _Last)
  48. : _Mybase(key_compare(), allocator_type())
  49. { // construct map from [_First, _Last), defaults
  50. _DEBUG_RANGE(_First, _Last);
  51. for (; _First != _Last; ++_First)
  52. this->insert(*_First);
  53. }
  54.  
  55. template<class _Iter>
  56. map(_Iter _First, _Iter _Last,
  57. const key_compare& _Pred)
  58. : _Mybase(_Pred, allocator_type())
  59. { // construct map from [_First, _Last), comparator
  60. _DEBUG_RANGE(_First, _Last);
  61. for (; _First != _Last; ++_First)
  62. this->insert(*_First);
  63. }
  64.  
  65. template<class _Iter>
  66. map(_Iter _First, _Iter _Last,
  67. const key_compare& _Pred, const allocator_type& _Al)
  68. : _Mybase(_Pred, _Al)
  69. { // construct map from [_First, _Last), comparator, and allocator
  70. _DEBUG_RANGE(_First, _Last);
  71. for (; _First != _Last; ++_First)
  72. this->insert(*_First);
  73. }
  74.  
  75. #if _HAS_STRICT_CONFORMANCE
  76. void erase(iterator _Where)
  77. { // erase element at _Where
  78. _Mybase::erase(_Where);
  79. }
  80.  
  81. size_type erase(const key_type& _Keyval)
  82. { // erase and count all that match _Keyval
  83. return (_Mybase::erase(_Keyval));
  84. }
  85.  
  86. void erase(iterator _First, iterator _Last)
  87. { // erase [_First, _Last)
  88. _Mybase::erase(_First, _Last);
  89. }
  90. #endif /* _HAS_STRICT_CONFORMANCE */
  91.  
  92. mapped_type& operator[](const key_type& _Keyval)
  93. { // find element matching _Keyval or insert with default mapped
  94. iterator _Where = this->lower_bound(_Keyval);
  95. if (_Where == this->end()
  96. || this->comp(_Keyval, this->_Key(_Where._Mynode())))
  97. _Where = this->insert(_Where,
  98. value_type(_Keyval, mapped_type()));
  99. return ((*_Where).second);
  100. }
  101. };

less的定义

  1. template<class _Ty>
  2. struct less
  3. : public binary_function<_Ty, _Ty, bool>
  4. { // functor for operator<
  5. bool operator()(const _Ty& _Left, const _Ty& _Right) const
  6. { // apply operator< to operands
  7. return (_Left < _Right);
  8. }
  9. };

从上面定义可以看出,map<_Kty, _Ty, _Pr, _Alloc>的后两个默认的参数,class _Pr = less<_Kty> , class _Alloc = allocator<pair<const _Kty, _Ty> > >;

而默认缺省定义map时,此时如果结构体为key值时,而此时class _Pr = less<_Kty>要进行_Left < _Right,而如果此时结构体没有<方法时,则会出错。

解决上面问题有两种方法:1.在结构体重重载<操作符;2.定义自己的排序函数,显式调用。

1.结构体中重载<

  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5. struct StructTest
  6. {
  7. string sTemp;
  8. int nCount;
  9. bool operator<(const StructTest& test) const
  10. {
  11. if (sTemp < test.sTemp)
  12. {
  13. return true;
  14. }
  15. else if (sTemp == test.sTemp)
  16. {
  17. if (nCount < test.nCount)
  18. return true;
  19. else
  20. return false;
  21. }
  22. else
  23. {
  24. return false;
  25. }
  26. }
  27. };
  28. void main()
  29. {
  30. map<StructTest,string>mapTest;
  31. map<StructTest,string>::iterator mapTestiter;
  32. StructTest StructTest1,StructTest2,StructTest3;
  33. StructTest1.sTemp="111111";
  34. StructTest1.nCount=6;
  35. StructTest2.sTemp="111111";
  36. StructTest2.nCount=7;
  37. StructTest3.sTemp="000000";
  38. StructTest3.nCount=6;
  39. mapTest.insert(make_pair(StructTest1,"1"));
  40. mapTest.insert(make_pair(StructTest2,"2"));
  41. mapTest.insert(make_pair(StructTest3,"3"));
  42. for (mapTestiter=mapTest.begin();mapTestiter!=mapTest.end();mapTestiter++)
  43. {
  44. cout<<(mapTestiter->first).sTemp<<" "<<(mapTestiter->first).nCount<<" "<<mapTestiter->second<<endl;
  45. }
  46.  
  47. }

2.定义自己的排序函数,显式调用

  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5. struct StructTest
  6. {
  7. string sTemp;
  8. int nCount;
  9. };
  10.  
  11. struct ptr_less : public binary_function<StructTest,StructTest,bool>
  12. {
  13. bool operator()(const StructTest& test1, const StructTest& test2) const
  14. {
  15. if (test1.sTemp < test2.sTemp)
  16. {
  17. return true;
  18. }
  19. else if (test1.sTemp == test2.sTemp)
  20. {
  21. if (test1.nCount < test2.nCount)
  22. return true;
  23. else
  24. return false;
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30. }
  31. };
  32.  
  33. void main()
  34. {
  35. map<StructTest,string,ptr_less>mapTest;
  36. map<StructTest,string,ptr_less>::iterator mapTestiter;
  37. StructTest StructTest1,StructTest2,StructTest3;
  38. StructTest1.sTemp="111111";
  39. StructTest1.nCount=6;
  40. StructTest2.sTemp="111111";
  41. StructTest2.nCount=7;
  42. StructTest3.sTemp="000000";
  43. StructTest3.nCount=6;
  44. mapTest.insert(make_pair(StructTest1,"1"));
  45. mapTest.insert(make_pair(StructTest2,"2"));
  46. mapTest.insert(make_pair(StructTest3,"3"));
  47. for (mapTestiter=mapTest.begin();mapTestiter!=mapTest.end();mapTestiter++)
  48. {
  49. cout<<(mapTestiter->first).sTemp<<" "<<(mapTestiter->first).nCount<<" "<<mapTestiter->second<<endl;
  50. }
  51.  
  52. }

当然其中的find方法也与_Pr有关。

map中find调用红黑树的find

具体调用如下:1.map中的find

2.红黑树中find

注意:STL中的SET与map一样。

具体调用可以查看STL源码剖析、或者vs中查看相关定义

STL map、set中key为结构体的用法的更多相关文章

  1. 剔除list中相同的结构体数据

    剔除list中相同的结构体数据,有三个思路:1.两层循环,逐个比较 2.使用set容器来剔除 3.使用unique方法去重 // deduplication.cpp : 定义控制台应用程序的入口点. ...

  2. MFC中的NMHDR结构体和NMUPDOWN结构体

    建立spin控件,创建UDN_DELTAPOS一个消息函数后: void CSpinDlg::OnDeltaposSpin1(NMHDR* pNMHDR, LRESULT* pResult) { NM ...

  3. 获取map集合中key、value

    获取Map集合类中key.value的两种方法 方法一:利用Set集合中的keySet()方法 Map<String,String> map = new HashMap<String ...

  4. C语言中 不定义结构体变量求成员大小

    所谓的求成员大小, 是求成员在该结构体中 用 sizeof(结构体名.结构体成员名) 求来的. 很多时候我们需要知道一个结构体成员中的某个成员的大小, 但是我们又不需要定义该结构体类型的变量(定义的话 ...

  5. 过滤掉map集合中key或value为空的值

    package cn.com.utils; import org.apache.commons.lang3.StringUtils; import java.util.Collection; impo ...

  6. 如何系统学习C 语言(中)之 结构体篇

    1,结构体 在前面我们知道变量和数组都可以用来存储数据,变量用来存储单个数据,数组可以用来存储一组同类型的数据,但你有没有发现--它们都只适合单一属性的数据.那现实生活中,很多对象都是具有多属性的.例 ...

  7. 在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  8. Linux中网络通信中 使用的结构体

    "+++++++++++++++++++++++++ Linux TCP/UDP通信中的结构体 +++++++++++++++++++++++++++++++++++++++" s ...

  9. 【2016-08-18】转载:总结C++中几种结构体初始化的方法

    作者:Ac_Von 博客地址:http://www.cnblogs.com/vongang/ 文章地址:http://www.cnblogs.com/vongang/archive/2011/07/3 ...

随机推荐

  1. 如何解决thinkphp5中验证码常见问题?

    对于thinkphp如何实现验证码,我这里就不介绍了.直接看之前的文章 http://www.cnblogs.com/qqblog/p/6639419.html.下面,我能想出来的是,我自己在开发过程 ...

  2. 继上一篇bootstrap框架(首页)弄的资讯页面

    还是和上一篇首页一样给出每一步的注解: 做的有点简单,但是,以后还是会加深的.毕竟是初学者嘛! <html lang="zh-cn">   <head>   ...

  3. 46.VUE学习之--组件之使用动态组件灵活设置页面布局

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  5. Ubuntu中搭建Hadoop集群(简记)

    stp1:在Vmware虚拟机上创建Ubantu.2环境 步骤:文件—>新建虚拟机—>典型(下一步)—>下一步——>位置(不建议放c盘,文件地址一定要全英文)—>下一步— ...

  6. Python的re模块的常用方法

    一.re的match与search方法 1.re.match方法 re.match 尝试从字符串的起始位置匹配一个模式,匹配成功re.match方法返回一个匹配的对象,如果不是起始位置匹配成功的话,m ...

  7. Develop Android Game Using Cocos2d-x

    0. Environment Windows 7 x64Visual Studio 2013adt-bundle-windows-x86 (http://developer.android.com/s ...

  8. Qt Qwdget 汽车仪表知识点拆解3 进度条编写

    先贴上效果图,注意,没有写逻辑,都是乱动的 这篇我来说说左侧的这个进度条的实现原理,其实更简单,哈哈哈 有一个大的widget,根据素材,我放了10个label 剩下的就是写一个函数,根据数据的不同, ...

  9. deepin linux 安装/启动jeakins报错:处理

    ERROR: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin 安装报错: 1.如还未安装java,则安装 ...

  10. 一款代码高亮插件 -- SyntaxHighlighter

    SyntaxHighlighter 是当前用得最多的一款代码高亮插件,包括本博客也用到了该插件来显示代码,大家可以看到效果了.只不过这是针对WordPress的一款代码高亮插件,而今天我要给大家介绍的 ...