STL source code analysis --- stl_map.h Reading Notes

Source: Internet
Author: User

Map is a standard relational container in STL. It stores pair elements with key-value keys and real-value values. Store the data in the red/black tree according to the key value. Two elements with the same key value are not allowed in map. When an element is inserted into the red/black tree, the key-value key cannot be modified, but the value can be modified because the map iterator is not a constant iterator, but a mutable iterator.

G ++ 2.91.57, Cygnus \ cygwin-b20 \ include \ G ++ \ stl_map.h complete list/*** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and modify this software * and its documentation for any purpose is hereby granted without handle, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting Documentation. hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * ** copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. ** permission to use, copy, modify, distribute and merge this software * and its documentation for any purpose is hereby granted without tables ,* Provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. silicon Graphics makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * // * Note: This is an internal header file, encoded ded by other STL Header S. * You shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_map_h # DEFINE _ alias # If defined (_ SGI )&&! Defined (_ gnuc _) & (_ mips_sim! = _ Mips_sim_abi32) # pragma set woff 1174 # endif # ifndef _ stl_limited_default_templates // key is the key-value type, and T is the real-value data type template <class key, class t, class compare = less <key>, class alloc = alloc> # elsetemplate <class key, class T, class compare, class alloc = alloc> # endifclass map {public: // typedefs: typedef key key_type; // key value type typedef t data_type; // real value, data type typedef t mapped_type; // typedef pair <const key, T> value_type; // element type (key value/true value) typedef compare key_compare; // A comparison function of key value // The following defines a functo, which calls the element comparison function. Class value_compare: Public binary_function <value_type, value_type, bool> {friend class Map <key, T, compare, alloc>; protected: Compare comp; value_compare (compare C ): comp (c) {} public: // overload () is the element comparison function bool operator () (const value_type & X, const value_type & Y) const {return comp (X. first, Y. first) ;}}; PRIVATE: // use the first type of Map (A Pair) as the key value type of the TB-tree. // Therefore, in the Rb-tree, the key-value key cannot be modified, but the value data can be modified. Typedef rb_tree <key_type, value_type, select1st <value_type>, key_compare, alloc> rep_type; rep_type t; // The Red/black tree acts as the underlying data structure public: typedef typename rep_type: pointer; typedef typename rep_type: const_pointer; typedef typename rep_type: Reference reference; typedef typename rep_type: const_reference; // const_iterator not defined as Rb-tree. I think the key value key cannot be modified, // but the value data can be modified. Typedef typename rep_type: iterator; typedef typename rep_type: Optional bytes; typedef typename rep_type: Required bytes; typedef typename rep_type: difference_type; // allocation/deallocation // note that map must use insert_un Ique () instead of insert_equal (). // Use insert_equal () only for multimap (). Map (): T (compare () {} explicit map (const compare & Comp): T (COMP) {}# ifdef _ stl_member_templates template <class inputiterator> map (inputiterator first, inputiterator last): T (compare () {T. insert_unique (first, last);} template <class inputiterator> 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);} # E Ndif/* _ stl_member_templates */Map (const Map <key, T, compare, alloc> & X): T (X. t) {} Map <key, T, compare, alloc> & operator = (const Map <key, T, compare, alloc> & X) {T = x. t; return * This;} // accessors: // all the following map operation rows are provided by the RB-tree, so you only need to call the map operation. 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. RBE Gin ();} 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 () ;}// reload []. The return value is T & operator [] (const key_type & K) {return (* (insert (value_type (K, T ()))). first )). second;} void swap (Map <key, T, compare, alloc> & X) {T. Swap (X. t);} // insert/Erase // note that the following insert statements return the pair <iterator, bool> 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 <class inputiterator> 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. cl Ear ();} // 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 <iterator, iterator> pai_range (const key_type & X) {return T. pai_range (x);} pair <const_iterator, const_iterator> pai_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 <class key, class T, class compare, class alloc> inline bool operator ==( const Map <key, t, compare, alloc> & X, const Map <key, T, compare, alloc> & Y) {return X. T = y. t;} template <class key, class T, class compare, class alloc> inline bool operator <(const Map <key, T, compare, alloc> & X, const Map <key, T, compare, alloc> & Y) {return X. T <Y. t ;}# ifdef _ s Tl_function_tmpl_partial_ordertemplate <class key, class T, class compare, class alloc> inline void swap (Map <key, T, compare, alloc> & X, Map <key, T, compare, alloc> & 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:


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.