STL source code analysis container stl_set.h

Source: Internet
Author: User

This article is senlie original, reproduced Please retain this address: http://blog.csdn.net/zhengsenlie


Set

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

All elements are automatically sorted based on their key values.
You cannot use the set iterator to change the set element value. Because the set element value is its key value, it is related to the arrangement rules of the Set element.
Set <t>: iterator is defined as the const_iterator of the underlying RB-tree to prevent write operations.
The standard STL set uses RB-tree as the underlying mechanism, just as the stack uses deque as the underlying mechanism.

Multiset is basically the same as set, except that the insert_equal () of the underlying RB-tree is called during insertion, which allows repeated elements.

Example:
struct ltstr{  bool operator()(const char* s1, const char* s2) const  {    return strcmp(s1, s2) < 0;  }};int main(){  const int N = 6;  const char* a[N] = {"isomer", "ephemeral", "prosaic",                       "nugatory", "artichoke", "serif"};  const char* b[N] = {"flat", "this", "artichoke",                      "frigate", "prosaic", "isomer"};  set<const char*, ltstr> A(a, a + N);  set<const char*, ltstr> B(b, b + N);  set<const char*, ltstr> C;  cout << "Set A: ";  copy(A.begin(), A.end(), ostream_iterator<const char*>(cout, " "));  cout << endl;  cout << "Set B: ";  copy(B.begin(), B.end(), ostream_iterator<const char*>(cout, " "));     cout << endl;  cout << "Union: ";  set_union(A.begin(), A.end(), B.begin(), B.end(),            ostream_iterator<const char*>(cout, " "),            ltstr());     cout << endl;  cout << "Intersection: ";  set_intersection(A.begin(), A.end(), B.begin(), B.end(),                   ostream_iterator<const char*>(cout, " "),                   ltstr());      cout << endl;  set_difference(A.begin(), A.end(), B.begin(), B.end(),                 inserter(C, C.begin()),                 ltstr());  cout << "Set C (difference of A and B): ";  copy(C.begin(), C.end(), ostream_iterator<const char*>(cout, " "));  cout << endl;}

Source code:
# Ifndef _ sgi_stl_internal_set_h # DEFINE _ sgi_stl_internal_set_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 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 have the same typedef key key_type; typedef key value_type; // key_compare and value_compare also use the same comparison function typedef compare key_compare; t Ypedef compare value_compare; private: // The bottom layer uses the red/black tree to implement set --> see Objective C ++, this uses the composition is-implimented-in-terms-of function typedef rb_tree <key_type, value_type, identity <value_type>, key_compare, alloc> rep_type; rep_type T; // red-black tree representing setpublic: typedef typename rep_type: const_pointer pointer; typedef typename rep_type: const_pointer; typedef typename rep_t Ype: const_reference reference; typedef typename rep_type: const_reference; // The iterator of the set is defined as the const_iterator of the red/black tree. This indicates that the Set iterator cannot perform write operations. // This is because the set elements are arranged in a certain order and users are not allowed to perform the write operation in any place. typedef typename rep_type: const_iterator iterator; typedef typename rep_type: const_iterator; typedef typename rep_type: incorrect identifier; typedef typename rep_type: size_type; typedef typename rep_type: difference_type ;// Allocation/deallocation // set must use the insert-unique () of the RB-tree. In this way, when you want to insert a // existing key value, you will choose to ignore set (): T (compare () {}// pass the function object generated by compare () to the underlying red/black tree as the comparison function set during initialization. explicit set (const compare & Comp ): T (COMP) {}# ifdef _ stl_member_templates template <class inputiterator> set (inputiterator first, inputiterator last): T (compare () {T. insert_unique (first, last);} template <class inputiterator> set (inputit Erator 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_uni Que (first, last);} set (const_iterator first, const_iterator last, const compare & Comp): T (COMP) {T. insert_unique (first, last) ;}# 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; // calls the operator = function return * This;} of the underlying red/black tree. // all the following set operation behaviors are provided by the RB-tree, so set only needs to be called. // accessors: Key_compare key_comp () const {return T. key_comp ();} 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 m Ax_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_unique (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_templates */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: 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. pai_range (x);} frien D 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, compare, 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:


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.