multimap使用和排序


如果键值是string型,直接输出就可以了,multimap是排好序了的,如果你要用C风格字符串,就自定义一个排序规则,需要在创建 multimap的时候做:

  1. #include <map>  
  2. #include <iostream>  
  3. #include <cstring>  
  4. using namespace std;  
  5.   
  6. struct cstrcmp_less  
  7. {  
  8.     bool operator () (const char* a, const char* b)  
  9.     {  
  10.         return strcmp(a, b) == -1 ? 1 : 0;  
  11.     }  
  12. };  
  13.   
  14. int main()  
  15. {  
  16.     multimap<const char*, const char*, cstrcmp_less> xx;  
  17.     xx.insert(make_pair("12","22"));  
  18.     xx.insert(make_pair("23","22"));  
  19.     xx.insert(make_pair("14","22"));  
  20.     xx.insert(make_pair("11","22"));  
  21.       
  22.     for(multimap<const char*, const char*>::iterator it = xx.begin(); it != xx.end(); ++it)  
  23.         cout << it->first << endl;  
  24. }  
#include <map>
#include <iostream>
#include <cstring>
using namespace std;

struct cstrcmp_less
{
    bool operator () (const char* a, const char* b)
    {
        return strcmp(a, b) == -1 ? 1 : 0;
    }
};

int main()
{
    multimap<const char*, const char*, cstrcmp_less> xx;
    xx.insert(make_pair("12","22"));
    xx.insert(make_pair("23","22"));
    xx.insert(make_pair("14","22"));
    xx.insert(make_pair("11","22"));
    
    for(multimap<const char*, const char*>::iterator it = xx.begin(); it != xx.end(); ++it)
        cout << it->first << endl;
}

  1. C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。  
  2.   map和set的树建立之时就会自动排好序,之前使用map,觉得按value排序很麻烦,要建一个multimap将原map倒置存储。如何只用一个map实现value的排序呢?将key 和 value建一个结构体,再将结构体作为key建一个map。  
  3.   而排序当然要写比较函数,这个又如何解决呢?之前没去想map构造map<T1, T2>时,其实是有个默认的比较函数 map<T1, T2, less<T1>> ,即按key的升序排列,我们可以重写比较函数来实现我们想要的排序(value)。假设string是key, int是value;  
  4.   代码如下:  
  5.   #include<iostream>  
  6.   #include<map>  
  7.   #include<string>  
  8.   using namespace std;  
  9.   struct A  
  10.   {  
  11.   string s;  
  12.   int n;  
  13.   };  
  14.   class KeyComp  
  15.   {  
  16.   public:  
  17.   bool operator()(const A& a1, const A& a2) const  
  18.   {  
  19.   return (a1.n < a2.n ||(a1.n == a2.n && a1.s < a2.s));  
  20.   }  
  21.   };  
  22.   int main()  
  23.   {  
  24.   map<A, int, KeyComp> m;  
  25.   map<A, int, KeyComp>::iterator it;  
  26.   A rec;  
  27.   while(cin >> rec.s >> rec.n)  
  28.   {  
  29.   m[rec]++;  
  30.   }  
  31.   for(it = m.begin(); it != m.end(); it++)  
  32.   {  
  33.   cout << it->first.s << " " << it->first.n << " " << it->second;  
  34.   cout << endl;  
  35.   }  
  36.   return 0;  
  37.   }  
  38.   #include<iostream>  
  39.   #include<map>  
  40.   #include<string>  
  41.   using namespace std;  
  42.   struct A  
  43.   {  
  44.   string s;  
  45.   int n;  
  46.   };  
  47.   class KeyComp  
  48.   {  
  49.   public:  
  50.   bool operator()(const A& a1, const A& a2) const  
  51.   {  
  52.   return (a1.n < a2.n ||(a1.n == a2.n && a1.s < a2.s));  
  53.   }  
  54.   };  
  55.   int main()  
  56.   {  
  57.   map<A, int, KeyComp> m;  
  58.   map<A, int, KeyComp>::iterator it;  
  59.   A rec;  
  60.   while(cin >> rec.s >> rec.n)  
  61.   {  
  62.   m[rec]++;  
  63.   }  
  64.   for(it = m.begin(); it != m.end(); it++)  
  65.   {  
  66.   cout << it->first.s << " " << it->first.n << " " << it->second;  
  67.   cout << endl;  
  68.   }  
  69.   return 0;  
  70.   }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值