map的键使用自定义结构体

背景:

 

map的定义

 

template<class Key, class T, class Pred = less<Key>, class A = allocator<T> >

 

可见,map的定义是一个模板类,模板参数为Key,T,Pred,A,各参数的含义如下:

Key---------键

T------------值

Pred--------map用于比较键的类,这个类必须重载()操作符,在其中定义比较操作..(虽然比较怪,不是重载大于小于号,但的确是这样定义的)

A------------内存分配

 

注:由于map是采用了红黑树,而红黑树在操作节点时都需要依赖于比较节点间的大小,所以键采用自定义结构体的条件就是得让系统可以对其进行比较.可以通过两种方法提供这种功能:

1. 为结构体重载<操作符

2. 不需要为结构体重载<操作符.但需要提供一个Pred类,且该类提供了operator()重载.实际上通过class Pred = less<Key>可以看出,map缺省使用系统提供的less类.

 

以下为两种方案的例子:

1.为自定义类型重载operator<,map的第三个参数为默认仿函数less<key>。
例子:

  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using   namespace  std;
  5. class  test
  6. {
  7. public :
  8.      bool  operator<( const  test& a) const ;
  9. //private:
  10.      int  nA;
  11.      int  nB;
  12. };
  13. bool  test::operator<( const  test& a) const
  14. {
  15.      if ( this ->nA < a.nA)
  16.          return   true ;
  17.      else
  18.     {
  19.          if ( this ->nA == a.nA &&  this ->nB < a.nB)
  20.              return   true ;
  21.          else
  22.              return   false ;
  23.     }
  24. }
  25. int  main()
  26. {
  27.     map<test, string> myTestDemo;
  28.     test tA;
  29.     tA.nA = 1;
  30.     tA.nB = 1;
  31.     test tB;
  32.     tB.nA = 1;
  33.     tB.nB = 2;
  34.     myTestDemo.insert(pair<test, string>(tA,  "first!" ));
  35.     myTestDemo.insert(pair<test, string>(tB,  "second!" ));
  36.     map<test, string>::iterator myItr = myTestDemo.begin();
  37.     cout <<  "itr begin test nA:"  << myItr->first.nA << endl;
  38.     cout <<  "itr begin test nB:"  << myItr->first.nB << endl;
  39.     cout <<  "itr begin test string:"  << myItr->second << endl;
  40.      return  1;
  41. }

特别注意: operator < 函数的详细内容,一定要满足strict weakly ordering,不能单纯的返回false或true

可以参考:http://www.softwelt.com/Know/KnowDetail-113762.html

 

2.不使用map的第三个参数为默认仿函数less<key>,自己编写一个比较仿函数。

  1. #include <iostream>
  2. #include <map>
  3. using   namespace  std;
  4. struct  keyOfMap
  5. {
  6.      int  firstOfKey;
  7.      int  secondOfKey;
  8. };
  9. struct  myMapFunctor
  10. {
  11.      bool  operator()( const  keyOfMap& k1,  const  keyOfMap& k2)  const
  12.     {
  13.          if (k1.firstOfKey < k2.firstOfKey)
  14.              return   true ;
  15.          else
  16.              return   false ;
  17.     }
  18. };
  19. int  main()
  20. {
  21.     map<keyOfMap, string, myMapFunctor> test;
  22.     keyOfMap temp1;
  23.     keyOfMap temp2;
  24.     temp1.firstOfKey = 1;
  25.     temp1.secondOfKey = 1;
  26.     temp2.firstOfKey = 2;
  27.     temp2.secondOfKey = 2;
  28.     test.insert(make_pair<keyOfMap, string>(temp1,  "first" ));
  29.     test.insert(make_pair<keyOfMap, string>(temp2,  "second" ));
  30.     map<keyOfMap, string, myMapFunctor>::iterator begin = test.begin();
  31.     cout << begin->first.firstOfKey << begin->first.secondOfKey << begin->second << endl;
  32.      return  1;
注: 以上所述同样适用于multimap

另,只要有了上述实现,map也可以使用find函数了,不需要重载==
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值