libstdc++
stl_set.h
Go to the documentation of this file.
00001 // Set implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  *
00027  * Copyright (c) 1994
00028  * Hewlett-Packard Company
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Hewlett-Packard Company makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  *
00039  * Copyright (c) 1996,1997
00040  * Silicon Graphics Computer Systems, Inc.
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Silicon Graphics makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  */
00050 
00051 /** @file bits/stl_set.h
00052  *  This is an internal header file, included by other library headers.
00053  *  Do not attempt to use it directly. @headername{set}
00054  */
00055 
00056 #ifndef _STL_SET_H
00057 #define _STL_SET_H 1
00058 
00059 #include <bits/concept_check.h>
00060 #if __cplusplus >= 201103L
00061 #include <initializer_list>
00062 #endif
00063 
00064 namespace std _GLIBCXX_VISIBILITY(default)
00065 {
00066 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00067 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
00068 
00069   template<typename _Key, typename _Compare, typename _Alloc>
00070     class multiset;
00071 
00072   /**
00073    *  @brief A standard container made up of unique keys, which can be
00074    *  retrieved in logarithmic time.
00075    *
00076    *  @ingroup associative_containers
00077    *
00078    *  @tparam _Key  Type of key objects.
00079    *  @tparam _Compare  Comparison function object type, defaults to less<_Key>.
00080    *  @tparam _Alloc  Allocator type, defaults to allocator<_Key>.
00081    *
00082    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00083    *  <a href="tables.html#66">reversible container</a>, and an
00084    *  <a href="tables.html#69">associative container</a> (using unique keys).
00085    *
00086    *  Sets support bidirectional iterators.
00087    *
00088    *  The private tree data is declared exactly the same way for set and
00089    *  multiset; the distinction is made entirely in how the tree functions are
00090    *  called (*_unique versus *_equal, same as the standard).
00091   */
00092   template<typename _Key, typename _Compare = std::less<_Key>,
00093            typename _Alloc = std::allocator<_Key> >
00094     class set
00095     {
00096 #ifdef _GLIBCXX_CONCEPT_CHECKS
00097       // concept requirements
00098       typedef typename _Alloc::value_type               _Alloc_value_type;
00099 # if __cplusplus < 201103L
00100       __glibcxx_class_requires(_Key, _SGIAssignableConcept)
00101 # endif
00102       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
00103                                 _BinaryFunctionConcept)
00104       __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
00105 #endif
00106 
00107 #if __cplusplus >= 201103L
00108       static_assert(is_same<typename remove_cv<_Key>::type, _Key>::value,
00109           "std::set must have a non-const, non-volatile value_type");
00110 # ifdef __STRICT_ANSI__
00111       static_assert(is_same<typename _Alloc::value_type, _Key>::value,
00112           "std::set must have the same value_type as its allocator");
00113 # endif
00114 #endif
00115 
00116     public:
00117       // typedefs:
00118       //@{
00119       /// Public typedefs.
00120       typedef _Key     key_type;
00121       typedef _Key     value_type;
00122       typedef _Compare key_compare;
00123       typedef _Compare value_compare;
00124       typedef _Alloc   allocator_type;
00125       //@}
00126 
00127     private:
00128       typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
00129         rebind<_Key>::other _Key_alloc_type;
00130 
00131       typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
00132                        key_compare, _Key_alloc_type> _Rep_type;
00133       _Rep_type _M_t;  // Red-black tree representing set.
00134 
00135       typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits;
00136 
00137     public:
00138       //@{
00139       ///  Iterator-related typedefs.
00140       typedef typename _Alloc_traits::pointer            pointer;
00141       typedef typename _Alloc_traits::const_pointer      const_pointer;
00142       typedef typename _Alloc_traits::reference          reference;
00143       typedef typename _Alloc_traits::const_reference    const_reference;
00144       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00145       // DR 103. set::iterator is required to be modifiable,
00146       // but this allows modification of keys.
00147       typedef typename _Rep_type::const_iterator         iterator;
00148       typedef typename _Rep_type::const_iterator         const_iterator;
00149       typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
00150       typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00151       typedef typename _Rep_type::size_type              size_type;
00152       typedef typename _Rep_type::difference_type        difference_type;
00153       //@}
00154 
00155 #if __cplusplus > 201402L
00156       using node_type = typename _Rep_type::node_type;
00157       using insert_return_type = typename _Rep_type::insert_return_type;
00158 #endif
00159 
00160       // allocation/deallocation
00161       /**
00162        *  @brief  Default constructor creates no elements.
00163        */
00164 #if __cplusplus < 201103L
00165       set() : _M_t() { }
00166 #else
00167       set() = default;
00168 #endif
00169 
00170       /**
00171        *  @brief  Creates a %set with no elements.
00172        *  @param  __comp  Comparator to use.
00173        *  @param  __a  An allocator object.
00174        */
00175       explicit
00176       set(const _Compare& __comp,
00177           const allocator_type& __a = allocator_type())
00178       : _M_t(__comp, _Key_alloc_type(__a)) { }
00179 
00180       /**
00181        *  @brief  Builds a %set from a range.
00182        *  @param  __first  An input iterator.
00183        *  @param  __last  An input iterator.
00184        *
00185        *  Create a %set consisting of copies of the elements from
00186        *  [__first,__last).  This is linear in N if the range is
00187        *  already sorted, and NlogN otherwise (where N is
00188        *  distance(__first,__last)).
00189        */
00190       template<typename _InputIterator>
00191         set(_InputIterator __first, _InputIterator __last)
00192         : _M_t()
00193         { _M_t._M_insert_unique(__first, __last); }
00194 
00195       /**
00196        *  @brief  Builds a %set from a range.
00197        *  @param  __first  An input iterator.
00198        *  @param  __last  An input iterator.
00199        *  @param  __comp  A comparison functor.
00200        *  @param  __a  An allocator object.
00201        *
00202        *  Create a %set consisting of copies of the elements from
00203        *  [__first,__last).  This is linear in N if the range is
00204        *  already sorted, and NlogN otherwise (where N is
00205        *  distance(__first,__last)).
00206        */
00207       template<typename _InputIterator>
00208         set(_InputIterator __first, _InputIterator __last,
00209             const _Compare& __comp,
00210             const allocator_type& __a = allocator_type())
00211         : _M_t(__comp, _Key_alloc_type(__a))
00212         { _M_t._M_insert_unique(__first, __last); }
00213 
00214       /**
00215        *  @brief  %Set copy constructor.
00216        *
00217        *  Whether the allocator is copied depends on the allocator traits.
00218        */
00219 #if __cplusplus < 201103L
00220       set(const set& __x)
00221       : _M_t(__x._M_t) { }
00222 #else
00223       set(const set&) = default;
00224 
00225      /**
00226        *  @brief %Set move constructor
00227        *
00228        *  The newly-created %set contains the exact contents of the moved
00229        *  instance. The moved instance is a valid, but unspecified, %set.
00230        */
00231       set(set&&) = default;
00232 
00233       /**
00234        *  @brief  Builds a %set from an initializer_list.
00235        *  @param  __l  An initializer_list.
00236        *  @param  __comp  A comparison functor.
00237        *  @param  __a  An allocator object.
00238        *
00239        *  Create a %set consisting of copies of the elements in the list.
00240        *  This is linear in N if the list is already sorted, and NlogN
00241        *  otherwise (where N is @a __l.size()).
00242        */
00243       set(initializer_list<value_type> __l,
00244           const _Compare& __comp = _Compare(),
00245           const allocator_type& __a = allocator_type())
00246       : _M_t(__comp, _Key_alloc_type(__a))
00247       { _M_t._M_insert_unique(__l.begin(), __l.end()); }
00248 
00249       /// Allocator-extended default constructor.
00250       explicit
00251       set(const allocator_type& __a)
00252       : _M_t(_Compare(), _Key_alloc_type(__a)) { }
00253 
00254       /// Allocator-extended copy constructor.
00255       set(const set& __x, const allocator_type& __a)
00256       : _M_t(__x._M_t, _Key_alloc_type(__a)) { }
00257 
00258       /// Allocator-extended move constructor.
00259       set(set&& __x, const allocator_type& __a)
00260       noexcept(is_nothrow_copy_constructible<_Compare>::value
00261                && _Alloc_traits::_S_always_equal())
00262       : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { }
00263 
00264       /// Allocator-extended initialier-list constructor.
00265       set(initializer_list<value_type> __l, const allocator_type& __a)
00266       : _M_t(_Compare(), _Key_alloc_type(__a))
00267       { _M_t._M_insert_unique(__l.begin(), __l.end()); }
00268 
00269       /// Allocator-extended range constructor.
00270       template<typename _InputIterator>
00271         set(_InputIterator __first, _InputIterator __last,
00272             const allocator_type& __a)
00273         : _M_t(_Compare(), _Key_alloc_type(__a))
00274         { _M_t._M_insert_unique(__first, __last); }
00275 
00276       /**
00277        *  The dtor only erases the elements, and note that if the elements
00278        *  themselves are pointers, the pointed-to memory is not touched in any
00279        *  way. Managing the pointer is the user's responsibility.
00280        */
00281       ~set() = default;
00282 #endif
00283 
00284       /**
00285        *  @brief  %Set assignment operator.
00286        *
00287        *  Whether the allocator is copied depends on the allocator traits.
00288        */
00289 #if __cplusplus < 201103L
00290       set&
00291       operator=(const set& __x)
00292       {
00293         _M_t = __x._M_t;
00294         return *this;
00295       }
00296 #else
00297       set&
00298       operator=(const set&) = default;
00299 
00300       /// Move assignment operator.
00301       set&
00302       operator=(set&&) = default;
00303 
00304       /**
00305        *  @brief  %Set list assignment operator.
00306        *  @param  __l  An initializer_list.
00307        *
00308        *  This function fills a %set with copies of the elements in the
00309        *  initializer list @a __l.
00310        *
00311        *  Note that the assignment completely changes the %set and
00312        *  that the resulting %set's size is the same as the number
00313        *  of elements assigned.
00314        */
00315       set&
00316       operator=(initializer_list<value_type> __l)
00317       {
00318         _M_t._M_assign_unique(__l.begin(), __l.end());
00319         return *this;
00320       }
00321 #endif
00322 
00323       // accessors:
00324 
00325       ///  Returns the comparison object with which the %set was constructed.
00326       key_compare
00327       key_comp() const
00328       { return _M_t.key_comp(); }
00329       ///  Returns the comparison object with which the %set was constructed.
00330       value_compare
00331       value_comp() const
00332       { return _M_t.key_comp(); }
00333       ///  Returns the allocator object with which the %set was constructed.
00334       allocator_type
00335       get_allocator() const _GLIBCXX_NOEXCEPT
00336       { return allocator_type(_M_t.get_allocator()); }
00337 
00338       /**
00339        *  Returns a read-only (constant) iterator that points to the first
00340        *  element in the %set.  Iteration is done in ascending order according
00341        *  to the keys.
00342        */
00343       iterator
00344       begin() const _GLIBCXX_NOEXCEPT
00345       { return _M_t.begin(); }
00346 
00347       /**
00348        *  Returns a read-only (constant) iterator that points one past the last
00349        *  element in the %set.  Iteration is done in ascending order according
00350        *  to the keys.
00351        */
00352       iterator
00353       end() const _GLIBCXX_NOEXCEPT
00354       { return _M_t.end(); }
00355 
00356       /**
00357        *  Returns a read-only (constant) iterator that points to the last
00358        *  element in the %set.  Iteration is done in descending order according
00359        *  to the keys.
00360        */
00361       reverse_iterator
00362       rbegin() const _GLIBCXX_NOEXCEPT
00363       { return _M_t.rbegin(); }
00364 
00365       /**
00366        *  Returns a read-only (constant) reverse iterator that points to the
00367        *  last pair in the %set.  Iteration is done in descending order
00368        *  according to the keys.
00369        */
00370       reverse_iterator
00371       rend() const _GLIBCXX_NOEXCEPT
00372       { return _M_t.rend(); }
00373 
00374 #if __cplusplus >= 201103L
00375       /**
00376        *  Returns a read-only (constant) iterator that points to the first
00377        *  element in the %set.  Iteration is done in ascending order according
00378        *  to the keys.
00379        */
00380       iterator
00381       cbegin() const noexcept
00382       { return _M_t.begin(); }
00383 
00384       /**
00385        *  Returns a read-only (constant) iterator that points one past the last
00386        *  element in the %set.  Iteration is done in ascending order according
00387        *  to the keys.
00388        */
00389       iterator
00390       cend() const noexcept
00391       { return _M_t.end(); }
00392 
00393       /**
00394        *  Returns a read-only (constant) iterator that points to the last
00395        *  element in the %set.  Iteration is done in descending order according
00396        *  to the keys.
00397        */
00398       reverse_iterator
00399       crbegin() const noexcept
00400       { return _M_t.rbegin(); }
00401 
00402       /**
00403        *  Returns a read-only (constant) reverse iterator that points to the
00404        *  last pair in the %set.  Iteration is done in descending order
00405        *  according to the keys.
00406        */
00407       reverse_iterator
00408       crend() const noexcept
00409       { return _M_t.rend(); }
00410 #endif
00411 
00412       ///  Returns true if the %set is empty.
00413       bool
00414       empty() const _GLIBCXX_NOEXCEPT
00415       { return _M_t.empty(); }
00416 
00417       ///  Returns the size of the %set.
00418       size_type
00419       size() const _GLIBCXX_NOEXCEPT
00420       { return _M_t.size(); }
00421 
00422       ///  Returns the maximum size of the %set.
00423       size_type
00424       max_size() const _GLIBCXX_NOEXCEPT
00425       { return _M_t.max_size(); }
00426 
00427       /**
00428        *  @brief  Swaps data with another %set.
00429        *  @param  __x  A %set of the same element and allocator types.
00430        *
00431        *  This exchanges the elements between two sets in constant
00432        *  time.  (It is only swapping a pointer, an integer, and an
00433        *  instance of the @c Compare type (which itself is often
00434        *  stateless and empty), so it should be quite fast.)  Note
00435        *  that the global std::swap() function is specialized such
00436        *  that std::swap(s1,s2) will feed to this function.
00437        *
00438        *  Whether the allocators are swapped depends on the allocator traits.
00439        */
00440       void
00441       swap(set& __x)
00442       _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
00443       { _M_t.swap(__x._M_t); }
00444 
00445       // insert/erase
00446 #if __cplusplus >= 201103L
00447       /**
00448        *  @brief Attempts to build and insert an element into the %set.
00449        *  @param __args  Arguments used to generate an element.
00450        *  @return  A pair, of which the first element is an iterator that points
00451        *           to the possibly inserted element, and the second is a bool
00452        *           that is true if the element was actually inserted.
00453        *
00454        *  This function attempts to build and insert an element into the %set.
00455        *  A %set relies on unique keys and thus an element is only inserted if
00456        *  it is not already present in the %set.
00457        *
00458        *  Insertion requires logarithmic time.
00459        */
00460       template<typename... _Args>
00461         std::pair<iterator, bool>
00462         emplace(_Args&&... __args)
00463         { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
00464 
00465       /**
00466        *  @brief Attempts to insert an element into the %set.
00467        *  @param  __pos  An iterator that serves as a hint as to where the
00468        *                element should be inserted.
00469        *  @param  __args  Arguments used to generate the element to be
00470        *                 inserted.
00471        *  @return An iterator that points to the element with key equivalent to
00472        *          the one generated from @a __args (may or may not be the
00473        *          element itself).
00474        *
00475        *  This function is not concerned about whether the insertion took place,
00476        *  and thus does not return a boolean like the single-argument emplace()
00477        *  does.  Note that the first parameter is only a hint and can
00478        *  potentially improve the performance of the insertion process.  A bad
00479        *  hint would cause no gains in efficiency.
00480        *
00481        *  For more on @a hinting, see:
00482        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
00483        *
00484        *  Insertion requires logarithmic time (if the hint is not taken).
00485        */
00486       template<typename... _Args>
00487         iterator
00488         emplace_hint(const_iterator __pos, _Args&&... __args)
00489         {
00490           return _M_t._M_emplace_hint_unique(__pos,
00491                                              std::forward<_Args>(__args)...);
00492         }
00493 #endif
00494 
00495       /**
00496        *  @brief Attempts to insert an element into the %set.
00497        *  @param  __x  Element to be inserted.
00498        *  @return  A pair, of which the first element is an iterator that points
00499        *           to the possibly inserted element, and the second is a bool
00500        *           that is true if the element was actually inserted.
00501        *
00502        *  This function attempts to insert an element into the %set.  A %set
00503        *  relies on unique keys and thus an element is only inserted if it is
00504        *  not already present in the %set.
00505        *
00506        *  Insertion requires logarithmic time.
00507        */
00508       std::pair<iterator, bool>
00509       insert(const value_type& __x)
00510       {
00511         std::pair<typename _Rep_type::iterator, bool> __p =
00512           _M_t._M_insert_unique(__x);
00513         return std::pair<iterator, bool>(__p.first, __p.second);
00514       }
00515 
00516 #if __cplusplus >= 201103L
00517       std::pair<iterator, bool>
00518       insert(value_type&& __x)
00519       {
00520         std::pair<typename _Rep_type::iterator, bool> __p =
00521           _M_t._M_insert_unique(std::move(__x));
00522         return std::pair<iterator, bool>(__p.first, __p.second);
00523       }
00524 #endif
00525 
00526       /**
00527        *  @brief Attempts to insert an element into the %set.
00528        *  @param  __position  An iterator that serves as a hint as to where the
00529        *                    element should be inserted.
00530        *  @param  __x  Element to be inserted.
00531        *  @return An iterator that points to the element with key of
00532        *           @a __x (may or may not be the element passed in).
00533        *
00534        *  This function is not concerned about whether the insertion took place,
00535        *  and thus does not return a boolean like the single-argument insert()
00536        *  does.  Note that the first parameter is only a hint and can
00537        *  potentially improve the performance of the insertion process.  A bad
00538        *  hint would cause no gains in efficiency.
00539        *
00540        *  For more on @a hinting, see:
00541        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
00542        *
00543        *  Insertion requires logarithmic time (if the hint is not taken).
00544        */
00545       iterator
00546       insert(const_iterator __position, const value_type& __x)
00547       { return _M_t._M_insert_unique_(__position, __x); }
00548 
00549 #if __cplusplus >= 201103L
00550       iterator
00551       insert(const_iterator __position, value_type&& __x)
00552       { return _M_t._M_insert_unique_(__position, std::move(__x)); }
00553 #endif
00554 
00555       /**
00556        *  @brief A template function that attempts to insert a range
00557        *  of elements.
00558        *  @param  __first  Iterator pointing to the start of the range to be
00559        *                   inserted.
00560        *  @param  __last  Iterator pointing to the end of the range.
00561        *
00562        *  Complexity similar to that of the range constructor.
00563        */
00564       template<typename _InputIterator>
00565         void
00566         insert(_InputIterator __first, _InputIterator __last)
00567         { _M_t._M_insert_unique(__first, __last); }
00568 
00569 #if __cplusplus >= 201103L
00570       /**
00571        *  @brief Attempts to insert a list of elements into the %set.
00572        *  @param  __l  A std::initializer_list<value_type> of elements
00573        *               to be inserted.
00574        *
00575        *  Complexity similar to that of the range constructor.
00576        */
00577       void
00578       insert(initializer_list<value_type> __l)
00579       { this->insert(__l.begin(), __l.end()); }
00580 #endif
00581 
00582 #if __cplusplus > 201402L
00583       /// Extract a node.
00584       node_type
00585       extract(const_iterator __pos)
00586       {
00587         __glibcxx_assert(__pos != end());
00588         return _M_t.extract(__pos);
00589       }
00590 
00591       /// Extract a node.
00592       node_type
00593       extract(const key_type& __x)
00594       { return _M_t.extract(__x); }
00595 
00596       /// Re-insert an extracted node.
00597       insert_return_type
00598       insert(node_type&& __nh)
00599       { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
00600 
00601       /// Re-insert an extracted node.
00602       iterator
00603       insert(const_iterator __hint, node_type&& __nh)
00604       { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
00605 
00606       template<typename, typename>
00607         friend class std::_Rb_tree_merge_helper;
00608 
00609       template<typename _Compare1>
00610         void
00611         merge(set<_Key, _Compare1, _Alloc>& __source)
00612         {
00613           using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
00614           _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
00615         }
00616 
00617       template<typename _Compare1>
00618         void
00619         merge(set<_Key, _Compare1, _Alloc>&& __source)
00620         { merge(__source); }
00621 
00622       template<typename _Compare1>
00623         void
00624         merge(multiset<_Key, _Compare1, _Alloc>& __source)
00625         {
00626           using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
00627           _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
00628         }
00629 
00630       template<typename _Compare1>
00631         void
00632         merge(multiset<_Key, _Compare1, _Alloc>&& __source)
00633         { merge(__source); }
00634 #endif // C++17
00635 
00636 #if __cplusplus >= 201103L
00637       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00638       // DR 130. Associative erase should return an iterator.
00639       /**
00640        *  @brief Erases an element from a %set.
00641        *  @param  __position  An iterator pointing to the element to be erased.
00642        *  @return An iterator pointing to the element immediately following
00643        *          @a __position prior to the element being erased. If no such
00644        *          element exists, end() is returned.
00645        *
00646        *  This function erases an element, pointed to by the given iterator,
00647        *  from a %set.  Note that this function only erases the element, and
00648        *  that if the element is itself a pointer, the pointed-to memory is not
00649        *  touched in any way.  Managing the pointer is the user's
00650        *  responsibility.
00651        */
00652       _GLIBCXX_ABI_TAG_CXX11
00653       iterator
00654       erase(const_iterator __position)
00655       { return _M_t.erase(__position); }
00656 #else
00657       /**
00658        *  @brief Erases an element from a %set.
00659        *  @param  position  An iterator pointing to the element to be erased.
00660        *
00661        *  This function erases an element, pointed to by the given iterator,
00662        *  from a %set.  Note that this function only erases the element, and
00663        *  that if the element is itself a pointer, the pointed-to memory is not
00664        *  touched in any way.  Managing the pointer is the user's
00665        *  responsibility.
00666        */
00667       void
00668       erase(iterator __position)
00669       { _M_t.erase(__position); }
00670 #endif
00671 
00672       /**
00673        *  @brief Erases elements according to the provided key.
00674        *  @param  __x  Key of element to be erased.
00675        *  @return  The number of elements erased.
00676        *
00677        *  This function erases all the elements located by the given key from
00678        *  a %set.
00679        *  Note that this function only erases the element, and that if
00680        *  the element is itself a pointer, the pointed-to memory is not touched
00681        *  in any way.  Managing the pointer is the user's responsibility.
00682        */
00683       size_type
00684       erase(const key_type& __x)
00685       { return _M_t.erase(__x); }
00686 
00687 #if __cplusplus >= 201103L
00688       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00689       // DR 130. Associative erase should return an iterator.
00690       /**
00691        *  @brief Erases a [__first,__last) range of elements from a %set.
00692        *  @param  __first  Iterator pointing to the start of the range to be
00693        *                 erased.
00694 
00695        *  @param __last Iterator pointing to the end of the range to
00696        *  be erased.
00697        *  @return The iterator @a __last.
00698        *
00699        *  This function erases a sequence of elements from a %set.
00700        *  Note that this function only erases the element, and that if
00701        *  the element is itself a pointer, the pointed-to memory is not touched
00702        *  in any way.  Managing the pointer is the user's responsibility.
00703        */
00704       _GLIBCXX_ABI_TAG_CXX11
00705       iterator
00706       erase(const_iterator __first, const_iterator __last)
00707       { return _M_t.erase(__first, __last); }
00708 #else
00709       /**
00710        *  @brief Erases a [first,last) range of elements from a %set.
00711        *  @param  __first  Iterator pointing to the start of the range to be
00712        *                 erased.
00713        *  @param __last Iterator pointing to the end of the range to
00714        *  be erased.
00715        *
00716        *  This function erases a sequence of elements from a %set.
00717        *  Note that this function only erases the element, and that if
00718        *  the element is itself a pointer, the pointed-to memory is not touched
00719        *  in any way.  Managing the pointer is the user's responsibility.
00720        */
00721       void
00722       erase(iterator __first, iterator __last)
00723       { _M_t.erase(__first, __last); }
00724 #endif
00725 
00726       /**
00727        *  Erases all elements in a %set.  Note that this function only erases
00728        *  the elements, and that if the elements themselves are pointers, the
00729        *  pointed-to memory is not touched in any way.  Managing the pointer is
00730        *  the user's responsibility.
00731        */
00732       void
00733       clear() _GLIBCXX_NOEXCEPT
00734       { _M_t.clear(); }
00735 
00736       // set operations:
00737 
00738       //@{
00739       /**
00740        *  @brief  Finds the number of elements.
00741        *  @param  __x  Element to located.
00742        *  @return  Number of elements with specified key.
00743        *
00744        *  This function only makes sense for multisets; for set the result will
00745        *  either be 0 (not present) or 1 (present).
00746        */
00747       size_type
00748       count(const key_type& __x) const
00749       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00750 
00751 #if __cplusplus > 201103L
00752       template<typename _Kt>
00753         auto
00754         count(const _Kt& __x) const
00755         -> decltype(_M_t._M_count_tr(__x))
00756         { return _M_t._M_count_tr(__x); }
00757 #endif
00758       //@}
00759 
00760       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00761       // 214.  set::find() missing const overload
00762       //@{
00763       /**
00764        *  @brief Tries to locate an element in a %set.
00765        *  @param  __x  Element to be located.
00766        *  @return  Iterator pointing to sought-after element, or end() if not
00767        *           found.
00768        *
00769        *  This function takes a key and tries to locate the element with which
00770        *  the key matches.  If successful the function returns an iterator
00771        *  pointing to the sought after element.  If unsuccessful it returns the
00772        *  past-the-end ( @c end() ) iterator.
00773        */
00774       iterator
00775       find(const key_type& __x)
00776       { return _M_t.find(__x); }
00777 
00778       const_iterator
00779       find(const key_type& __x) const
00780       { return _M_t.find(__x); }
00781 
00782 #if __cplusplus > 201103L
00783       template<typename _Kt>
00784         auto
00785         find(const _Kt& __x)
00786         -> decltype(iterator{_M_t._M_find_tr(__x)})
00787         { return iterator{_M_t._M_find_tr(__x)}; }
00788 
00789       template<typename _Kt>
00790         auto
00791         find(const _Kt& __x) const
00792         -> decltype(const_iterator{_M_t._M_find_tr(__x)})
00793         { return const_iterator{_M_t._M_find_tr(__x)}; }
00794 #endif
00795       //@}
00796 
00797       //@{
00798       /**
00799        *  @brief Finds the beginning of a subsequence matching given key.
00800        *  @param  __x  Key to be located.
00801        *  @return  Iterator pointing to first element equal to or greater
00802        *           than key, or end().
00803        *
00804        *  This function returns the first element of a subsequence of elements
00805        *  that matches the given key.  If unsuccessful it returns an iterator
00806        *  pointing to the first element that has a greater value than given key
00807        *  or end() if no such element exists.
00808        */
00809       iterator
00810       lower_bound(const key_type& __x)
00811       { return _M_t.lower_bound(__x); }
00812 
00813       const_iterator
00814       lower_bound(const key_type& __x) const
00815       { return _M_t.lower_bound(__x); }
00816 
00817 #if __cplusplus > 201103L
00818       template<typename _Kt>
00819         auto
00820         lower_bound(const _Kt& __x)
00821         -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
00822         { return iterator(_M_t._M_lower_bound_tr(__x)); }
00823 
00824       template<typename _Kt>
00825         auto
00826         lower_bound(const _Kt& __x) const
00827         -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
00828         { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
00829 #endif
00830       //@}
00831 
00832       //@{
00833       /**
00834        *  @brief Finds the end of a subsequence matching given key.
00835        *  @param  __x  Key to be located.
00836        *  @return Iterator pointing to the first element
00837        *          greater than key, or end().
00838        */
00839       iterator
00840       upper_bound(const key_type& __x)
00841       { return _M_t.upper_bound(__x); }
00842 
00843       const_iterator
00844       upper_bound(const key_type& __x) const
00845       { return _M_t.upper_bound(__x); }
00846 
00847 #if __cplusplus > 201103L
00848       template<typename _Kt>
00849         auto
00850         upper_bound(const _Kt& __x)
00851         -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
00852         { return iterator(_M_t._M_upper_bound_tr(__x)); }
00853 
00854       template<typename _Kt>
00855         auto
00856         upper_bound(const _Kt& __x) const
00857         -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
00858         { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
00859 #endif
00860       //@}
00861 
00862       //@{
00863       /**
00864        *  @brief Finds a subsequence matching given key.
00865        *  @param  __x  Key to be located.
00866        *  @return  Pair of iterators that possibly points to the subsequence
00867        *           matching given key.
00868        *
00869        *  This function is equivalent to
00870        *  @code
00871        *    std::make_pair(c.lower_bound(val),
00872        *                   c.upper_bound(val))
00873        *  @endcode
00874        *  (but is faster than making the calls separately).
00875        *
00876        *  This function probably only makes sense for multisets.
00877        */
00878       std::pair<iterator, iterator>
00879       equal_range(const key_type& __x)
00880       { return _M_t.equal_range(__x); }
00881 
00882       std::pair<const_iterator, const_iterator>
00883       equal_range(const key_type& __x) const
00884       { return _M_t.equal_range(__x); }
00885 
00886 #if __cplusplus > 201103L
00887       template<typename _Kt>
00888         auto
00889         equal_range(const _Kt& __x)
00890         -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
00891         { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
00892 
00893       template<typename _Kt>
00894         auto
00895         equal_range(const _Kt& __x) const
00896         -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
00897         { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
00898 #endif
00899       //@}
00900 
00901       template<typename _K1, typename _C1, typename _A1>
00902         friend bool
00903         operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
00904 
00905       template<typename _K1, typename _C1, typename _A1>
00906         friend bool
00907         operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
00908     };
00909 
00910 #if __cpp_deduction_guides >= 201606
00911 
00912   template<typename _InputIterator,
00913            typename _Compare =
00914              less<typename iterator_traits<_InputIterator>::value_type>,
00915            typename _Allocator =
00916              allocator<typename iterator_traits<_InputIterator>::value_type>,
00917            typename = _RequireInputIter<_InputIterator>,
00918            typename = _RequireAllocator<_Allocator>>
00919     set(_InputIterator, _InputIterator,
00920         _Compare = _Compare(), _Allocator = _Allocator())
00921     -> set<typename iterator_traits<_InputIterator>::value_type,
00922           _Compare, _Allocator>;
00923 
00924   template<typename _Key, typename _Compare = less<_Key>,
00925            typename _Allocator = allocator<_Key>,
00926            typename = _RequireAllocator<_Allocator>>
00927     set(initializer_list<_Key>,
00928         _Compare = _Compare(), _Allocator = _Allocator())
00929     -> set<_Key, _Compare, _Allocator>;
00930 
00931   template<typename _InputIterator, typename _Allocator,
00932            typename = _RequireInputIter<_InputIterator>,
00933            typename = _RequireAllocator<_Allocator>>
00934     set(_InputIterator, _InputIterator, _Allocator)
00935     -> set<typename iterator_traits<_InputIterator>::value_type,
00936            less<typename iterator_traits<_InputIterator>::value_type>,
00937            _Allocator>;
00938 
00939   template<typename _Key, typename _Allocator,
00940            typename = _RequireAllocator<_Allocator>>
00941     set(initializer_list<_Key>, _Allocator)
00942     -> set<_Key, less<_Key>, _Allocator>;
00943 
00944 #endif
00945 
00946   /**
00947    *  @brief  Set equality comparison.
00948    *  @param  __x  A %set.
00949    *  @param  __y  A %set of the same type as @a x.
00950    *  @return  True iff the size and elements of the sets are equal.
00951    *
00952    *  This is an equivalence relation.  It is linear in the size of the sets.
00953    *  Sets are considered equivalent if their sizes are equal, and if
00954    *  corresponding elements compare equal.
00955   */
00956   template<typename _Key, typename _Compare, typename _Alloc>
00957     inline bool
00958     operator==(const set<_Key, _Compare, _Alloc>& __x,
00959                const set<_Key, _Compare, _Alloc>& __y)
00960     { return __x._M_t == __y._M_t; }
00961 
00962   /**
00963    *  @brief  Set ordering relation.
00964    *  @param  __x  A %set.
00965    *  @param  __y  A %set of the same type as @a x.
00966    *  @return  True iff @a __x is lexicographically less than @a __y.
00967    *
00968    *  This is a total ordering relation.  It is linear in the size of the
00969    *  sets.  The elements must be comparable with @c <.
00970    *
00971    *  See std::lexicographical_compare() for how the determination is made.
00972   */
00973   template<typename _Key, typename _Compare, typename _Alloc>
00974     inline bool
00975     operator<(const set<_Key, _Compare, _Alloc>& __x,
00976               const set<_Key, _Compare, _Alloc>& __y)
00977     { return __x._M_t < __y._M_t; }
00978 
00979   ///  Returns !(x == y).
00980   template<typename _Key, typename _Compare, typename _Alloc>
00981     inline bool
00982     operator!=(const set<_Key, _Compare, _Alloc>& __x,
00983                const set<_Key, _Compare, _Alloc>& __y)
00984     { return !(__x == __y); }
00985 
00986   ///  Returns y < x.
00987   template<typename _Key, typename _Compare, typename _Alloc>
00988     inline bool
00989     operator>(const set<_Key, _Compare, _Alloc>& __x,
00990               const set<_Key, _Compare, _Alloc>& __y)
00991     { return __y < __x; }
00992 
00993   ///  Returns !(y < x)
00994   template<typename _Key, typename _Compare, typename _Alloc>
00995     inline bool
00996     operator<=(const set<_Key, _Compare, _Alloc>& __x,
00997                const set<_Key, _Compare, _Alloc>& __y)
00998     { return !(__y < __x); }
00999 
01000   ///  Returns !(x < y)
01001   template<typename _Key, typename _Compare, typename _Alloc>
01002     inline bool
01003     operator>=(const set<_Key, _Compare, _Alloc>& __x,
01004                const set<_Key, _Compare, _Alloc>& __y)
01005     { return !(__x < __y); }
01006 
01007   /// See std::set::swap().
01008   template<typename _Key, typename _Compare, typename _Alloc>
01009     inline void
01010     swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
01011     _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
01012     { __x.swap(__y); }
01013 
01014 _GLIBCXX_END_NAMESPACE_CONTAINER
01015 
01016 #if __cplusplus > 201402L
01017   // Allow std::set access to internals of compatible sets.
01018   template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
01019     struct
01020     _Rb_tree_merge_helper<_GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>, _Cmp2>
01021     {
01022     private:
01023       friend class _GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>;
01024 
01025       static auto&
01026       _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
01027       { return __set._M_t; }
01028 
01029       static auto&
01030       _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
01031       { return __set._M_t; }
01032     };
01033 #endif // C++17
01034 
01035 _GLIBCXX_END_NAMESPACE_VERSION
01036 } //namespace std
01037 #endif /* _STL_SET_H */