std::map 如何使用结构体作为自定义键值

转自:http://blog.csdn.net/u011417605/article/details/50895097

在使用map时,有时候我们需要自定义键值,才能符合程序的需要。

比如我们需要使用自定义的结构体来作为map的键值:

  1. struct  Test  
  2. {  
  3.     int x;  
  4.     int y;  
  5. };  

这样直接使用的话,在编译时会出问题:

  1. 1>------ Build started: Project: MapRemove, Configuration: Debug Win32 ------  
  2. 1>  Source.cpp  
  3. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::tuple<_Types...> &,const std::tuple<_Types1...> &)' : could not deduce template argument for 'const std::tuple<_Types...> &' from 'const Test'  
  4. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(480) : see declaration of 'std::operator <'  
  5. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(192) : while compiling class template member function 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const'  
  6. 1>          with  
  7. 1>          [  
  8. 1>              _Kty=Test  
  9. 1>  ,            _Ty=Test  
  10. 1>          ]  
  11. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\map(228) : see reference to function template instantiation 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const' being compiled  
  12. 1>          with  
  13. 1>          [  
  14. 1>              _Kty=Test  
  15. 1>  ,            _Ty=Test  
  16. 1>          ]  
  17. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(572) : see reference to class template instantiation 'std::less<_Kty>' being compiled  
  18. 1>          with  
  19. 1>          [  
  20. 1>              _Kty=Test  
  21. 1>          ]  
  22. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1023) : see reference to class template instantiation 'std::is_empty<std::less<_Kty>>' being compiled  
  23. 1>          with  
  24. 1>          [  
  25. 1>              _Kty=Test  
  26. 1>          ]  
  27. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\map(70) : see reference to class template instantiation 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>' being compiled  
  28. 1>          with  
  29. 1>          [  
  30. 1>              _Kty=Test  
  31. 1>  ,            _Ty=std::string  
  32. 1>  ,            _Pr=std::less<Test>  
  33. 1>  ,            _Alloc=std::allocator<std::pair<const Test,std::string>>  
  34. 1>          ]  
  35. 1>          f:\xdd\xaudio2\mapremove\source.cpp(12) : see reference to class template instantiation 'std::map<Test,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled  
  36. 1>          with  
  37. 1>          [  
  38. 1>              _Kty=Test  
  39. 1>  ,            _Ty=std::string  
  40. 1>          ]  
  41. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const Test'  
  42. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(2259) : see declaration of 'std::operator <'  
  43. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Test'  
  44. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(2545) : see declaration of 'std::operator <'  
  45. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const Test'  
  46. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(2535) : see declaration of 'std::operator <'  
  47. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Test'  
  48. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(2525) : see declaration of 'std::operator <'  
  49. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const Test'  
  50. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(1997) : see declaration of 'std::operator <'  
  51. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const Test'  
  52. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(1155) : see declaration of 'std::operator <'  
  53. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const Test'  
  54. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(971) : see declaration of 'std::operator <'  
  55. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const Test'  
  56. 1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\utility(230) : see declaration of 'std::operator <'  
  57. 1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2676: binary '<' : 'const Test' does not define this operator or a conversion to a type acceptable to the predefined operator  
  58. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  
看错误是说,键值无法比较。因为map的键值是自动比较后进插入的,键值是递增的。

现在我们自定义的键值,编译器无法进行比较,找不到类似的模板,所以报错。

既然是没有‘<’,那我们自己重载小于操作符应该就可以了:

  1. struct  Test  
  2. {  
  3.     int x;  
  4.     int y;  
  5.   
  6.     bool operator < (const Test &o) const  
  7.     {  
  8.         return x < o.x || y < o.y;  
  9.     }  
  10. };  
重载后,重新编译,顺利通过。测试代码如下:

  1. #include <map>  
  2. #include <iostream>  
  3.   
  4. struct  Test  
  5. {  
  6.     int x;  
  7.     int y;  
  8.   
  9.     bool operator < (const Test &o) const  
  10.     {  
  11.         return x < o.x || y < o.y;  
  12.     }  
  13. };  
  14.   
  15. int main()  
  16. {  
  17.     std::map<Test, std::string> mapTest;  
  18.     Test test = { 1, 2 };  
  19.     mapTest[test] = "Test1";  
  20.       
  21.     for (auto it = mapTest.begin(); it != mapTest.end();it++)  
  22.     {  
  23.         std::cout << it->first.x << " " << it->first.y << " " << it->second.c_str() << std::endl;  
  24.     }  
  25.   
  26.     return 0;  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值