STL source code analysis container stl_map.h

Source: Internet
Author: User

STL source code analysis container stl_map.h

 

Map

--------------------------------------------------------------------------------

All elements are automatically sorted based on their key values.
All the elements of map are pair and have both real and key values.
You cannot modify the key value of an element because it is related to the arrangement rules of map elements.
You can modify the real value of an element because it does not affect the arrangement rules of the map.
Map iterator is neither a constant iterators nor a mutable iterator
Standard STL map uses RB-tree as the underlying mechanism.

Multimap and map are basically the same, except that the insert_equal () of the underlying RB-tree is called during insertion, which allows repeated elements.

Figure 5-20

Example:
Struct ltstr {bool operator () (const char * s1, const char * s2) const {return strcmp (s1, s2) <0 ;}; int main () {map
 
  
Months; months [January] = 31; // two functions are called here. The map operator [] function returns the reference of the corresponding real value, call the operator = function of the real value type to assign values to months [February] = 28; months [march] = 31; months [1_l] = 30; months [may] = 31; months [June] = 30; months [July] = 31; months [August] = 31; months [September] = 30; months [October] = 31; months [November] = 30; months [December] = 31; cout <June-> <months [June] <endl; map
  
   
: Iterator cur = months. find (June); map
   
    
: Iterator prev = cur; map
    
     
: Iterator next = cur; ++ next; -- prev; cout <Previous (in alphabetical order) is <(* prev ). first <endl; cout <Next (in alphabetical order) is <(* next ). first <endl ;}
    
   
  
 


Source code:
// Pair definition template in stl_pair.h
 
  
Struct pair {typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair (): first (T1 (), second (T2 ()) {} pair (const T1 & a, const T2 & B): first (a), second (B) {}# ifdef _ STL_MEMBER_TEMPLATES template
  
   
Pair (const pair
   
    
& P): first (p. first), second (p. second) {}# endif };
   
  
 

// Stl_map.h source code # ifndef _ SGI_STL_INTERNAL_MAP_H # define _ sgi_stl_internal_map_h1_stl_begin_namespace # if defined (_ sgi )&&! Defined (_ GNUC _) & (_ MIPS_SIM! = _ MIPS_SIM_ABI32) # pragma set woff 1174 # endif # ifndef _ STL_LIMITED_DEFAULT_TEMPLATEStemplate
 
  
, Class Alloc = alloc> # elsetemplate
  
   
# Endifclass map {public: // typedefs: typedef Key key_type; // Key value type typedef T data_type; // Value Type typedef T mapped_type; typedef pair
   
    
Value_type; // element type (key value/real value) typedef Compare key_compare; // key-Value Comparison function // functor, which calls the element comparison function class value_compare: public binary_function
    
     
{Friend class map
     
      
; Protected: Compare comp; value_compare (Compare c): comp (c) {} public: bool operator () (const value_type & x, const value_type & y) const {return comp (x. first, y. first); // call the key-Value Comparison function with the key values of x and y}; private: typedef rb_tree
      
        , Key_compare, Alloc> rep_type; rep_type t; // mappublic: typedef typename rep_type: pointer; typedef typename rep_type :: reference; typedef typename rep_type: const_reference; // set iterator is defined as const, because it cannot change the value in the set. // The iterator of map is not defined as const, although the key value cannot be changed, the real value typedef typename rep_type: iterator; typedef typename rep_type: Required bytes; typedef typename rep_type:: commandid; typedef typename rep_type: size_type; typedef typename rep_type: difference_type; // allocation/deallocation map (): t (Compare ()) {} explicit map (const Compare & comp): t (comp) {}// pass Compare () the generated function object uses the underlying red/black tree as the comparison function set during initialization // duplicate key values are not allowed. Therefore, only the insert_unique () # ifdef _ STL_MEMBER_TEMPLATES template of the RB-tree can be used.
       
         Map (InputIterator first, InputIterator last): t (Compare () {t. insert_unique (first, last);} template
        
          Map (InputIterator first, InputIterator last, const Compare & comp): t (comp) {t. insert_unique (first, last) ;}# else map (const value_type * first, const value_type * last): t (Compare () {t. insert_unique (first, last);} map (const value_type * first, const value_type * last, const Compare & comp): t (comp) {t. insert_unique (first, last);} map (const_iterator first, const_iterator last): t (Compare () {t. insert_unique (first, last);} map (const_iterator first, const_iterator last, const Compare & comp): t (comp) {t. insert_unique (first, last);} # endif/* _ STL_MEMBER_TEMPLATES */map (const map
         
           & X): t (x. t) {} map
          
            & Amp; operator = (const map
           
             & X) {t = x. t; // calls the operator = function return * this;} of the underlying red/black tree. // all the following map operations are provided by the RB-tree, so map only needs to be called. // accessors: key_compare key_comp () const {return t. key_comp ();} value_compare value_comp () const {return value_compare (t. key_comp ();} iterator begin () {return t. begin ();} const_iterator begin () const {return t. begin ();} iterator end () {return t. end ();} const_iterator end () const {return t. end ();} reverse_iterator rbegin () {return t. rbegin ();} const_reverse_iterator rbegin () const {return t. rbegin ();} reverse_iterator rend () {return t. rend ();} const_reverse_iterator rend () const {return t. rend ();} bool empty () const {return t. empty ();} size_type size () const {return t. size ();} size_type max_size () const {return t. max_size ();} T & operator [] (const key_type & k) {return (* (insert (value_type (k, T ()))). first )). second;} void swap (map
            
              & X) {t. swap (x. t);} // insert/erase pair
             
               Insert (const value_type & x) {return t. insert_unique (x);} iterator insert (iterator position, const value_type & x) {return t. insert_unique (position, x);} # ifdef _ STL_MEMBER_TEMPLATES template
              
                Void insert (InputIterator first, InputIterator last) {t. insert_unique (first, last) ;}# else void insert (const value_type * first, const value_type * last) {t. insert_unique (first, last);} void insert (const_iterator first, const_iterator last) {t. insert_unique (first, last);} # endif/* _ STL_MEMBER_TEMPLATES */void erase (iterator position) {t. erase (position);} size_type erase (const key_type & x) {return t. erase (x);} void erase (iterator first, iterator last) {t. erase (first, last);} void clear () {t. clear ();} // map operations: iterator find (const key_type & x) {return t. find (x);} const_iterator find (const key_type & x) const {return t. find (x);} size_type count (const key_type & x) const {return t. count (x);} iterator lower_bound (const key_type & x) {return t. lower_bound (x);} const_iterator lower_bound (const key_type & x) const {return t. lower_bound (x);} iterator upper_bound (const key_type & x) {return t. upper_bound (x);} const_iterator upper_bound (const key_type & x) const {return t. upper_bound (x);} pair
               
                 Pai_range (const key_type & x) {return t. pai_range (x);} pair
                
                  Performance_range (const key_type & x) const {return t. performance_range (x);} friend bool operator ==_ _ STL_NULL_TMPL_ARGS (const map &, const map &); friend bool operator <_ STL_NULL_TMPL_ARGS (const map &, const map &) ;}; template
                 
                   Inline bool operator = (const map
                  
                    & Amp; x, const map
                   
                     & Y) {return x. t = y. t;} template
                    
                      Inline bool operator <(const map
                     
                       & Amp; x, const map
                      
                        & Y) {return x. t <y. t;} # ifdef _ STL_FUNCTION_TMPL_PARTIAL_ORDERtemplate
                       
                         Inline void swap (map
                        
                          & X, map
                         
                           & Y) {x. swap (y) ;}# endif/* _ STL_FUNCTION_TMPL_PARTIAL_ORDER */# if defined (_ sgi )&&! Defined (_ GNUC _) & (_ MIPS_SIM! = _ MIPS_SIM_ABI32) # pragma reset woff 1174 # endif _ STL_END_NAMESPACE # endif/* _ SGI_STL_INTERNAL_MAP_H * // Local Variables: // mode: C ++ // End:
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 


Related Article

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.