zoukankan      html  css  js  c++  java
  • C++ map

    为了实现快速查找,map内部本身就是按序存储的(比如红黑树)。在我们插入<key, value>键值对时,就会按照key的大小顺序进行存储。Map的定义:

    1 template < class Key, class T, class Compare = less<Key>, //入参为key
    2             //键         值                用于key比较的函数对象,与相对的greater
    3 class Allocator = allocator<pair<const Key,T> > > 
    4                      //用于存储分配
    5 class map; 

    其中less实现:

    1 template <class T> 
    2 struct less : binary_function <T,T,bool> 
    3 {  
    4   bool operator() (const T& x, const T& y) const  
    5     {return x<y;}  
    6 };  

    对Key排序

    由上可知,按key排序,只需自定义一个用于比较的类就ok了。

    例如:

     1 struct CmpByKeyLength
     2 {  
     3      bool operator()(const string& k1, const string& k2)
     4      {  
     5          return k1.length() < k2.length();  
     6       }  
     7 }; 
     8 int main()
     9 {  
    10     map<string, int, CmpByKeyLength> name_score_map;  
    11     name_score_map["LiMin"] = 90;    
    12     name_score_map.insert(make_pair("Bing",99));  
    13     name_score_map.insert(make_pair("Albert",86));  
    14     for (map<string, int>::iterator iter = name_score_map.begin();  
    15           iter != name_score_map.end();  
    16           ++iter) 
    17     {  
    18          cout << *iter << endl;  
    19     }
    20   
    21     return 0;  
    22 }    

     

     

    对Value排序

    对于value则既不能按Compare,也不可以用sort,因为sort只可用于序列(线性)容器,而map是集合容器(例如:红黑树)

    Map中的元素类型为pair

     1 template <class T1, class T2> struct pair  
     2 {  
     3   typedef T1 first_type;  
     4   typedef T2 second_type;  
     5   
     6   T1 first;               //对应Key
     7   T2 second;          //对应Value
     8  
     9   //构造函数
    10   pair() : first(T1()), second(T2()) {}  
    11   pair(const T1& x, const T2& y) : first(x), second(y) {}  
    12   template <class U, class V>  
    13   pair (const pair<U,V> &p) : first(p.first), second(p.second) { }  
    14 }  

    此外,在<utility>头文件中,还为pair重载了 <  运算符

    1 template<class _T1, class _T2>  
    2 inline bool  operator < (const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)  
    3 { 
    4     return __x.first < __y.first  ||                                 //key不同
    5   (!(__y.first < __x.first) && __x.second < __y.second);        //key相同,则比较value
    6 //为什么不用==呢?因为作为map的key必须实现<操作符的重载,至于==不能保证,而且浮点数…
    7 }  

    所以用map + vector即可实现对value 的排序

    例如:

     1 typedef pair< int, int> PAIR;  
     2 bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {  
     3   return lhs.second < rhs.second;  
     4 }  
     5 struct CmpByValue {  
     6   bool operator()(const PAIR& lhs, const PAIR& rhs) {  
     7     return lhs.second < rhs.second;  
     8   }  
     9 }; 
    10 int main() {  
    11   map<int, int> myMap;  
    12   myMap [1] = 90;  
    13   myMap [2] = 79;  
    14   myMap.insert(make_pair(3,99));  
    15   myMap insert(make_pair(4,86));  
    16  //把map中元素转存到vector中   
    17   vector<PAIR> name_score_vec(myMap.begin(), myMap.end());  
    18   sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue());  
    19   // sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value);  
    20   for (int i = 0; i != myMap.size(); ++i) {  
    21          cout << myMap [i] << endl;  
    22        }  
    23       return 0;  
    24  }  

    Sort算法如下:

    1 template <class RandomAccessIterator>  
    2 void sort ( RandomAccessIterator first, RandomAccessIterator last );  
    3   
    4 template <class RandomAccessIterator, class Compare>  
    5 void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); 
  • 相关阅读:
    html5基础--canvas标签元素
    html5基础--audio标签元素
    html5基础--video标签元素
    SSH Secure Shell Client中文乱码的解决方法
    Response.End() 与Response.Close()的区别
    服务器控件的返回值问题
    常用数据库操作(一)
    DataTable 读取数据库操作时去掉空格
    回车触发Button
    404页面自动跳转javascript
  • 原文地址:https://www.cnblogs.com/goAhead-hust/p/4632582.html
Copyright © 2011-2022 走看看