STL source code analysis-stl_multimap.h

Source: Internet
Author: User
// Filename: stl_multimap.h // comment by: Cream // E-mail: mdl2009@vip.qq.com // blog: http://blog.csdn.net/mdl13412/*** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and submit this software * and its documentation for any purpose is hereby granted without plugin, * provided that the above copyright notice appear in all copies and * that both that copyright n Otice 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 publish this software * and its affiliates Tation for any purpose is hereby granted without done, * 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 I S an internal header file, encoded ded by other STL headers. * You shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_multimap_h # DEFINE _ sgi_stl_internal_multimap_h1_stl_begin_namespace # If defined (_ SGI )&&! Defined (_ gnuc _) & (_ mips_sim! = _ Mips_sim_abi32) # pragma set woff 1174 # endif // If the compiler cannot deduce the default parameter type used later based on the preceding template parameters, you must manually specify it, in this implementation, the internal elements of multimap use less for comparison by default. // The internal maintenance data structure is a red/black tree, which has a very good time complexity in the worst case. // Note: Unlike map, multimap allows repeated elements # ifndef _ stl_limited_default_templatestemplate <class key, class T, class compare = less <key>, class alloc = alloc> # elsetemplate <class key, class t, class compare, class alloc = alloc> # endifclass multimap {public :/ /The definition is the same as that of map. For more information, see <setl_map.h> typedef key key_type; typedef t data_type; typedef t mapped_type; typedef pair <const key, T> value_type; typedef compare key_compare; // For more information about the inheritance from binary_function, see <stl_function.h>. // comparison of keys provided by Nested classes. Class value_compare: Public binary_function <value_type, value_type, bool> {friend class multimap <key, T, compare, alloc>; 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) ;}}; PRIVATE: // The data structure is represented by a red-black tree. In fact, <stl_tree.h> typedef rb_tree <key_type, value_type, select1st <value_type>, key_compare, alloc> rep_type; rep_type t; // red-black tree representing multimappublic: // typedefs marked as 'stl standard mandatory requests' are used to provide iterator_traits support // note: iterator, the reference type is designed as const, which is composed The nature of ltimap is determined. // if you change the value, the internal red/black tree may be faulty. typedef typename rep_type: pointer; // The STL standard enforces typedef typename rep_type: const_pointer pointer; typedef typename rep_type: Reference reference; // the STL standard enforces typedef typename rep_type: const_reference; typedef typename rep_type:: iterator; // the STL standard enforces typedef typename rep_type: const_iterator; typ Edef typename rep_type: includid; typedef typename rep_type: size_type; typedef typename rep_type: difference_type; // The STL standard enforces multimap (): T (compare () {} explicit multimap (const compare & Comp): T (COMP) {}# ifdef _ stl_member_templates template <class inputiterator> MUL Timap (inputiterator first, inputiterator last): T (compare () {T. insert_equal (first, last);} template <class inputiterator> multimap (inputiterator first, inputiterator last, const compare & Comp): T (COMP) {T. insert_equal (first, last) ;}# else multimap (const value_type * First, const value_type * Last): T (compare () {T. insert_equal (first, last);} multimap (const value_type * First, const value_typ E * Last, const compare & Comp): T (COMP) {T. insert_equal (first, last);} multimap (const_iterator first, const_iterator last): T (compare () {T. insert_equal (first, last);} multimap (const_iterator first, const_iterator last, const compare & Comp): T (COMP) {T. insert_equal (first, last) ;}# endif/* _ stl_member_templates */multimap (const multimap <key, T, compare, alloc> & X): T (X. t) {} multimap <Ke Y, T, compare, alloc> & operator = (const multimap <key, T, compare, alloc> & X) {T = x. t; return * This;} // return the key_compare key_comp () const {return T. key_comp () ;}// due to the nature of multimap, Value Comparison and key use the same comparison function 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 () ;}// A dedicated swap is called here, instead of a global swap, which is scheduled for <stl_tree.h> void swap (multimap <key, T, compare, alloc> & X) {T. swap (X. t);} // insert element. Note that the inserted element key allows repeated iterator insert (const value_type & X) {return T. insert_equal (x);} // insert an element at position, but position is only a prompt. If the given position cannot be inserted, // STL searches for it, this results in poor efficiency iterator insert (iterator position, const value_type & X) {return T. insert_equal (Position, x);} # ifdef _ stl_member_te Mplates template <class inputiterator> void insert (inputiterator first, inputiterator last) {T. insert_equal (first, last) ;}# else void insert (const value_type * First, const value_type * Last) {T. insert_equal (first, last);} void insert (const_iterator first, const_iterator last) {T. insert_equal (first, last) ;}# endif/* _ stl_member_templates * // erase the elements at the specified position, which causes the internal red/black tree to rearrange void erase (iterator posit Ion) {T. erase (position);} // returns the number of erased elements size_type erase (const key_type & X) {return T. erase (x);} // erase the elements in the specified range, which will lead to a large change in the red/black tree void erase (iterator first, iterator last) {T. erase (first, last);} // OK, clear all, goodbye, red/black tree void clear () {T. clear () ;}// find the specified iterator find (const key_type & X) {return T. find (x);} const_iterator find (const key_type & X) const {return T. find (x);} // returns the number of specified elements size_ty PE count (const key_type & X) const {return T. count (x);} // returns the 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);} // return the 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);} p Air <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 multimap &, const multimap &); friend bool operator <_ stl_null_tmpl_args (const multimap &, const multimap &) ;}; // compare two multimaps to compare the internal red and black trees, triggering operatortemplate of the red and black trees <CL Ass key, class T, class compare, class alloc> inline bool operator = (const multimap <key, T, compare, alloc> & X, const multimap <key, T, compare, alloc> & Y) {return X. T = y. t;} template <class key, class T, class compare, class alloc> inline bool operator <(const multimap <key, T, compare, alloc> & X, const multimap <key, T, compare, alloc> & Y) {return X. T <Y. t;} // If the compiler supports the special priority of template functions, implement global swap to use multi Map private swap to improve efficiency # ifdef _ stl_function_tmpl_partial_ordertemplate <class key, class T, class compare, class alloc> inline void swap (multimap <key, T, compare, alloc> & X, multimap <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_multimap_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.