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

Source: Internet
Author: User

Set is a standard container in STL. The elements in set are automatically sorted based on the key value. It does not have the correspondence between the real value and the key value as map does. Set only has the real value. When the underlying implementation of the set is RB-tree, the value of the set cannot be changed after it is inserted into the RB-tree, because the change means that the RB-tree feature may not be met, so its iterator set <t>: iterator is the constrant iterator of Rb-tree. Since the underlying layer of the set is RB-tree, the iterator will not expire after the set operation, but the iterator for deleting elements is an exception.


G ++ 2.91.57, Cygnus \ cygwin-b20 \ include \ G ++ \ stl_set.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_set_h # DEFINE _ alias # If defined (_ SGI )&&! Defined (_ gnuc _) & (_ mips_sim! = _ Mips_sim_abi32) # pragma set woff 1174 # endif/less <key> indicates that incremental sorting is used by default # ifndef _ stl_limited_default_templatestemplate <class key, class compare = less <key>, class alloc = alloc> # elsetemplate <class key, class compare, class alloc = alloc> # endifclass set {public: // typedefs: // The key_type and value_type types are both real-value typedef key key_type; typedef key value_type; // note that the following key_compare and value_compare use the same comparison function typedef Compare key_compare; typedef compare value_compare; private:/* Note: identity is defined in <stl_function.h>. For details, see chapter 7th, which is defined as template <class T> struct identity: public unary_function <t, t> {const T & operator () (const T & X) const {return x ;}; * // below, rb_tree <key, value, keyofvalue, compare, alloc> typedef rb_tree <key_type, value_type, identity <value_type>, key_compare, alloc> rep_type; rep_type t; // The bottom layer uses the red/black tree. Public: typedef typename rep_type: pointer; typedef typename rep_type: const_pointer pointer; typedef typename rep_type: const_reference Reference reference; typedef typename rep_type: const_reference; // use the const_iterator of the Rb-tree. You cannot modify the value or insert the element typedef typename rep_type: policiterator; typedef typename rep_type: const_iterator; typedef typename rep_type: Required parameter; typedef typename rep_type: incluconst_reverse_iterator; typedef typename rep_type: size_type; typedef typename rep_type: difference_type; // allocation/ Llocation // note that set must use insert_unique () instead of insert_equal (). // Multiset uses insert_equal (). Set (): T (compare () {} explicit set (const compare & Comp): T (COMP) {} // initialize set # ifdef _ stl_member_templates template <class inputiterator> set (inputiterator first, inputiterator last): T (compare () {T. insert_unique (first, last);} template <class inputiterator> set (inputiterator first, inputiterator last, const compare & Comp): T (COMP) {T. insert_unique (first, last);} # else set (const value_type * First, const value_type * Last): T (compare () {T. insert_unique (first, last);} set (const value_type * First, const value_type * Last, const compare & Comp): T (COMP) {T. insert_unique (first, last);} set (const_iterator first, const_iterator last): T (compare () {T. insert_unique (first, last);} set (const_iterator first, const_iterator last, const compare & Comp): T (COMP) {T. insert_unique (first, la St) ;}# endif/* _ stl_member_templates */set (const set <key, compare, alloc> & X): T (X. t) {} set <key, compare, alloc> & operator = (const set <key, compare, alloc> & X) {T = x. t; return * This;} // all the set operations are provided by the RB-tree. The set is only called. // accessors: key_compare key_comp () const {return T. key_comp () ;}// note that the value_comp () of the set is actually the key_comp () of the RB-tree (). Value_compare value_comp () const {return T. key_comp ();} iterator begin () const {return T. begin ();} iterator end () const {return T. end ();} reverse_iterator rbegin () const {return T. rbegin ();} 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 ();} void swap (Set <Key, compare, alloc> & X) {T. swap (X. t);} // insert/Erase typedef pair <iterator, bool> pair_iterator_bool; pair <iterator, bool> insert (const value_type & X) {pair <typename rep_type: iterator, bool> P = T. insert_unique (x); Return pair <iterator, bool> (P. first, P. second);} iterator insert (iterator position, const value_type & X) {typedef typename rep_type: iterator rep_iterator; return T. insert_uniq UE (rep_iterator &) position, x) ;}# ifdef _ stl_member_templates template <class inputiterator> void insert (inputiterator first, inputiterator last) {T. insert_unique (first, last) ;}# else void insert (const_iterator first, const_iterator last) {T. insert_unique (first, last);} void insert (const value_type * First, const value_type * Last) {T. insert_unique (first, last);} # endif/* _ stl_member_template S */void erase (iterator position) {typedef typename rep_type: iterator rep_iterator; T. erase (rep_iterator &) position);} size_type erase (const key_type & X) {return T. erase (x);} void erase (iterator first, iterator last) {typedef typename rep_type: iterator rep_iterator; T. erase (rep_iterator &) First, (rep_iterator &) Last);} void clear () {T. clear ();} // set operations: // use the RB-tree search function instead of the St L's find, STL's find only // sequential search, which is less efficient than associating containers defined by themselves. 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) const {return T. lower_bound (x);} iterator upper_bound (const key_type & X) const {return T. upper_bound (x);} pair <iterator, iterator> pai_range (const key_type & X) const {return T. performance_rang E (x);} friend bool operator ==_ _ stl_null_tmpl_args (const set &, const set &); friend bool operator <_ stl_null_tmpl_args (const set &, const set &) ;}; template <class key, class compare, class alloc> inline bool operator ==( const set <key, compare, alloc> & X, const set <key, compare, alloc> & Y) {return X. T = y. t;} template <class key, class compare, class alloc> inline bool operator <(const set <key, compa Re, alloc> & X, const set <key, compare, alloc> & Y) {return X. T <Y. t ;}# ifdef _ stl_function_tmpl_partial_ordertemplate <class key, class compare, class alloc> inline void swap (set <key, compare, alloc> & X, set <key, 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_set_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.