[C/C ++ standard library] _ [elementary] _ [map lookup Function Analysis]

Source: Internet
Author: User

[C/C ++ standard library] _ [elementary] _ [map lookup Function Analysis]

Scenario:

1. map is efficient in searching for objects with non-numeric indexes (which can be a vector for non-repeated numeric indexes), because the implementation of the Red/black tree makes logarithmic time efficient in searching and inserting.

2. map is a very practical data structure.

3. You can use multimap to store duplicate keys. The scenario is one-to-many. For example, a contact corresponds to multiple groups.

 

 

Void TestMap () {// map is generally implemented through the red/black tree. logarithmic timetypedef map
 
  
TMap; typedef multimap
  
   
TTMap; TMap m; m [1] = 8; m [2] = 10; m [5] = 11; TTMap mm; mm. insert (pair
   
    
(); Mm. insert (pair
    
     
(2, 10); mm. insert (pair
     
      
(1, 11); mm. insert (pair
      
        (1, 12); // countint count = 0; if (count = m. count (1) {// 1. if you want to use it, you still need to use [] to find it once. // output: m. count (1): 1 cout <"m. count (1): "<count <endl;} if (count = mm. count (1) {// 1. if you want to use it, you still need to use interval _range to find it once. // multimap does not have the [] OPERATOR overload, because the key is not unique. // output mm. count (1): 3 cout <"mm. count (1): "<count <endl;} // view the implementation of count, which is implemented using interval _range. // performance_range is a continuous iterator composed of all values equal to the key. // the first pair is the first iterator that matches the key, and the second is the first iterator that is greater than the key. // mappair
       
         Values = m. struct _range (1); TMap: iterator B = values. first; // output map struct _range: 8 while (B! = Values. second) {cout <"map into _range:" <(* B). second <endl; ++ B;} // multimappair
        
          Valuess = mm. required _range (1); TTMap: iterator bs = valuess. first; // output: // multimap upload _range: 8 // multimap upload _range: 11 // multimap upload _range: 12 while (bs! = Valuess. second) {cout <"multimap upload _range:" <(* bs ). second <endl; ++ bs;} // Find. Find the first iterator that matches the key. The second iterator cannot be found. If you need to Find the second iterator, use performance_range // Find to use lower_bound for search. lower_bound is used to Find the first element greater than or equal to the key. // similar to upper_bound to find the first element greater than the key. TMap: iterator ite = m. find (1); if (ite! = M. end () {// 1. use iterator directly. // output: m. find 8 cout <"m. find "<(* ite ). second <endl;} ite = m. lower_bound (3); if (ite! = M. end () {// output m. lower_bound key 5 m. lower_bound 11 cout <"m. lower_bound key "<(* ite ). first <"m. lower_bound "<(* ite ). second <endl;} // multimapbs = mm. lower_bound (1); if (ite! = M. end () {// output: mm. lower_bound key 1mm. lower_bound 8 cout <"mm. lower_bound key "<(* bs ). first <"mm. lower_bound "<(* bs ). second <endl;} bs = mm. upper_bound (1); if (ite! = M. end () {// output: mm. upper_bound key 2mm. upper_bound 10 cout <"mm. upper_bound key "<(* bs ). first <"mm. upper_bound "<(* bs ). second <endl ;}} int _ tmain (int argc, _ TCHAR * argv []) {TestMap (); return 0 ;}
        
       
      
     
    
   
  
 

 

Source code of the Standard Library:

 

size_type count(const key_type& _Keyval) const{// count all elements that match _Keyval_Paircc _Ans = equal_range(_Keyval);size_type _Num = 0;_Distance(_Ans.first, _Ans.second, _Num);return (_Num);}iterator find(const key_type& _Keyval){// find an element in mutable sequence that matches _Keyvaliterator _Where = lower_bound(_Keyval);return (_Where == end()|| _DEBUG_LT_PRED(this->comp,_Keyval, this->_Key(_Where._Mynode()))? end() : _Where);}


 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.