libstdc++
stl_set.h
Go to the documentation of this file.
1 // Set implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996,1997
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
51 /** @file bits/stl_set.h
52  * This is an internal header file, included by other library headers.
53  * Do not attempt to use it directly. @headername{set}
54  */
55 
56 #ifndef _STL_SET_H
57 #define _STL_SET_H 1
58 
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
63 
64 namespace std _GLIBCXX_VISIBILITY(default)
65 {
66 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
67 
68  /**
69  * @brief A standard container made up of unique keys, which can be
70  * retrieved in logarithmic time.
71  *
72  * @ingroup associative_containers
73  *
74  * @tparam _Key Type of key objects.
75  * @tparam _Compare Comparison function object type, defaults to less<_Key>.
76  * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
77  *
78  * Meets the requirements of a <a href="tables.html#65">container</a>, a
79  * <a href="tables.html#66">reversible container</a>, and an
80  * <a href="tables.html#69">associative container</a> (using unique keys).
81  *
82  * Sets support bidirectional iterators.
83  *
84  * The private tree data is declared exactly the same way for set and
85  * multiset; the distinction is made entirely in how the tree functions are
86  * called (*_unique versus *_equal, same as the standard).
87  */
88  template<typename _Key, typename _Compare = std::less<_Key>,
89  typename _Alloc = std::allocator<_Key> >
90  class set
91  {
92  // concept requirements
93  typedef typename _Alloc::value_type _Alloc_value_type;
94  __glibcxx_class_requires(_Key, _SGIAssignableConcept)
95  __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
96  _BinaryFunctionConcept)
97  __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
98 
99  public:
100  // typedefs:
101  //@{
102  /// Public typedefs.
103  typedef _Key key_type;
104  typedef _Key value_type;
105  typedef _Compare key_compare;
106  typedef _Compare value_compare;
107  typedef _Alloc allocator_type;
108  //@}
109 
110  private:
112  rebind<_Key>::other _Key_alloc_type;
113 
114  typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
115  key_compare, _Key_alloc_type> _Rep_type;
116  _Rep_type _M_t; // Red-black tree representing set.
117 
119 
120  public:
121  //@{
122  /// Iterator-related typedefs.
123  typedef typename _Alloc_traits::pointer pointer;
124  typedef typename _Alloc_traits::const_pointer const_pointer;
125  typedef typename _Alloc_traits::reference reference;
126  typedef typename _Alloc_traits::const_reference const_reference;
127  // _GLIBCXX_RESOLVE_LIB_DEFECTS
128  // DR 103. set::iterator is required to be modifiable,
129  // but this allows modification of keys.
130  typedef typename _Rep_type::const_iterator iterator;
131  typedef typename _Rep_type::const_iterator const_iterator;
134  typedef typename _Rep_type::size_type size_type;
135  typedef typename _Rep_type::difference_type difference_type;
136  //@}
137 
138  // allocation/deallocation
139  /**
140  * @brief Default constructor creates no elements.
141  */
142  set()
143  : _M_t() { }
144 
145  /**
146  * @brief Creates a %set with no elements.
147  * @param __comp Comparator to use.
148  * @param __a An allocator object.
149  */
150  explicit
151  set(const _Compare& __comp,
152  const allocator_type& __a = allocator_type())
153  : _M_t(__comp, _Key_alloc_type(__a)) { }
154 
155  /**
156  * @brief Builds a %set from a range.
157  * @param __first An input iterator.
158  * @param __last An input iterator.
159  *
160  * Create a %set consisting of copies of the elements from
161  * [__first,__last). This is linear in N if the range is
162  * already sorted, and NlogN otherwise (where N is
163  * distance(__first,__last)).
164  */
165  template<typename _InputIterator>
166  set(_InputIterator __first, _InputIterator __last)
167  : _M_t()
168  { _M_t._M_insert_unique(__first, __last); }
169 
170  /**
171  * @brief Builds a %set from a range.
172  * @param __first An input iterator.
173  * @param __last An input iterator.
174  * @param __comp A comparison functor.
175  * @param __a An allocator object.
176  *
177  * Create a %set consisting of copies of the elements from
178  * [__first,__last). This is linear in N if the range is
179  * already sorted, and NlogN otherwise (where N is
180  * distance(__first,__last)).
181  */
182  template<typename _InputIterator>
183  set(_InputIterator __first, _InputIterator __last,
184  const _Compare& __comp,
185  const allocator_type& __a = allocator_type())
186  : _M_t(__comp, _Key_alloc_type(__a))
187  { _M_t._M_insert_unique(__first, __last); }
188 
189  /**
190  * @brief %Set copy constructor.
191  * @param __x A %set of identical element and allocator types.
192  *
193  * The newly-created %set uses a copy of the allocation object used
194  * by @a __x.
195  */
196  set(const set& __x)
197  : _M_t(__x._M_t) { }
198 
199 #if __cplusplus >= 201103L
200  /**
201  * @brief %Set move constructor
202  * @param __x A %set of identical element and allocator types.
203  *
204  * The newly-created %set contains the exact contents of @a x.
205  * The contents of @a x are a valid, but unspecified %set.
206  */
207  set(set&& __x)
208  noexcept(is_nothrow_copy_constructible<_Compare>::value)
209  : _M_t(std::move(__x._M_t)) { }
210 
211  /**
212  * @brief Builds a %set from an initializer_list.
213  * @param __l An initializer_list.
214  * @param __comp A comparison functor.
215  * @param __a An allocator object.
216  *
217  * Create a %set consisting of copies of the elements in the list.
218  * This is linear in N if the list is already sorted, and NlogN
219  * otherwise (where N is @a __l.size()).
220  */
222  const _Compare& __comp = _Compare(),
223  const allocator_type& __a = allocator_type())
224  : _M_t(__comp, _Key_alloc_type(__a))
225  { _M_t._M_insert_unique(__l.begin(), __l.end()); }
226 
227  /// Allocator-extended default constructor.
228  explicit
229  set(const allocator_type& __a)
230  : _M_t(_Compare(), _Key_alloc_type(__a)) { }
231 
232  /// Allocator-extended copy constructor.
233  set(const set& __x, const allocator_type& __a)
234  : _M_t(__x._M_t, _Key_alloc_type(__a)) { }
235 
236  /// Allocator-extended move constructor.
237  set(set&& __x, const allocator_type& __a)
238  noexcept(is_nothrow_copy_constructible<_Compare>::value
239  && _Alloc_traits::_S_always_equal())
240  : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { }
241 
242  /// Allocator-extended initialier-list constructor.
244  : _M_t(_Compare(), _Key_alloc_type(__a))
245  { _M_t._M_insert_unique(__l.begin(), __l.end()); }
246 
247  /// Allocator-extended range constructor.
248  template<typename _InputIterator>
249  set(_InputIterator __first, _InputIterator __last,
250  const allocator_type& __a)
251  : _M_t(_Compare(), _Key_alloc_type(__a))
252  { _M_t._M_insert_unique(__first, __last); }
253 #endif
254 
255  /**
256  * @brief %Set assignment operator.
257  * @param __x A %set of identical element and allocator types.
258  *
259  * All the elements of @a __x are copied, but unlike the copy
260  * constructor, the allocator object is not copied.
261  */
262  set&
263  operator=(const set& __x)
264  {
265  _M_t = __x._M_t;
266  return *this;
267  }
268 
269 #if __cplusplus >= 201103L
270  /**
271  * @brief %Set move assignment operator.
272  * @param __x A %set of identical element and allocator types.
273  *
274  * The contents of @a __x are moved into this %set (without copying).
275  * @a __x is a valid, but unspecified %set.
276  */
277  set&
278  operator=(set&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
279  {
280  if (!_M_t._M_move_assign(__x._M_t))
281  {
282  // The rvalue's allocator cannot be moved and is not equal,
283  // so we need to individually move each element.
284  clear();
285  insert(std::__make_move_if_noexcept_iterator(__x._M_t.begin()),
286  std::__make_move_if_noexcept_iterator(__x._M_t.end()));
287  __x.clear();
288  }
289  return *this;
290  }
291 
292  /**
293  * @brief %Set list assignment operator.
294  * @param __l An initializer_list.
295  *
296  * This function fills a %set with copies of the elements in the
297  * initializer list @a __l.
298  *
299  * Note that the assignment completely changes the %set and
300  * that the resulting %set's size is the same as the number
301  * of elements assigned. Old data may be lost.
302  */
303  set&
305  {
306  this->clear();
307  this->insert(__l.begin(), __l.end());
308  return *this;
309  }
310 #endif
311 
312  // accessors:
313 
314  /// Returns the comparison object with which the %set was constructed.
316  key_comp() const
317  { return _M_t.key_comp(); }
318  /// Returns the comparison object with which the %set was constructed.
320  value_comp() const
321  { return _M_t.key_comp(); }
322  /// Returns the allocator object with which the %set was constructed.
324  get_allocator() const _GLIBCXX_NOEXCEPT
325  { return allocator_type(_M_t.get_allocator()); }
326 
327  /**
328  * Returns a read-only (constant) iterator that points to the first
329  * element in the %set. Iteration is done in ascending order according
330  * to the keys.
331  */
332  iterator
333  begin() const _GLIBCXX_NOEXCEPT
334  { return _M_t.begin(); }
335 
336  /**
337  * Returns a read-only (constant) iterator that points one past the last
338  * element in the %set. Iteration is done in ascending order according
339  * to the keys.
340  */
341  iterator
342  end() const _GLIBCXX_NOEXCEPT
343  { return _M_t.end(); }
344 
345  /**
346  * Returns a read-only (constant) iterator that points to the last
347  * element in the %set. Iteration is done in descending order according
348  * to the keys.
349  */
351  rbegin() const _GLIBCXX_NOEXCEPT
352  { return _M_t.rbegin(); }
353 
354  /**
355  * Returns a read-only (constant) reverse iterator that points to the
356  * last pair in the %set. Iteration is done in descending order
357  * according to the keys.
358  */
360  rend() const _GLIBCXX_NOEXCEPT
361  { return _M_t.rend(); }
362 
363 #if __cplusplus >= 201103L
364  /**
365  * Returns a read-only (constant) iterator that points to the first
366  * element in the %set. Iteration is done in ascending order according
367  * to the keys.
368  */
369  iterator
370  cbegin() const noexcept
371  { return _M_t.begin(); }
372 
373  /**
374  * Returns a read-only (constant) iterator that points one past the last
375  * element in the %set. Iteration is done in ascending order according
376  * to the keys.
377  */
378  iterator
379  cend() const noexcept
380  { return _M_t.end(); }
381 
382  /**
383  * Returns a read-only (constant) iterator that points to the last
384  * element in the %set. Iteration is done in descending order according
385  * to the keys.
386  */
389  { return _M_t.rbegin(); }
390 
391  /**
392  * Returns a read-only (constant) reverse iterator that points to the
393  * last pair in the %set. Iteration is done in descending order
394  * according to the keys.
395  */
397  crend() const noexcept
398  { return _M_t.rend(); }
399 #endif
400 
401  /// Returns true if the %set is empty.
402  bool
403  empty() const _GLIBCXX_NOEXCEPT
404  { return _M_t.empty(); }
405 
406  /// Returns the size of the %set.
407  size_type
408  size() const _GLIBCXX_NOEXCEPT
409  { return _M_t.size(); }
410 
411  /// Returns the maximum size of the %set.
412  size_type
413  max_size() const _GLIBCXX_NOEXCEPT
414  { return _M_t.max_size(); }
415 
416  /**
417  * @brief Swaps data with another %set.
418  * @param __x A %set of the same element and allocator types.
419  *
420  * This exchanges the elements between two sets in constant
421  * time. (It is only swapping a pointer, an integer, and an
422  * instance of the @c Compare type (which itself is often
423  * stateless and empty), so it should be quite fast.) Note
424  * that the global std::swap() function is specialized such
425  * that std::swap(s1,s2) will feed to this function.
426  */
427  void
428  swap(set& __x)
429 #if __cplusplus >= 201103L
430  noexcept(_Alloc_traits::_S_nothrow_swap())
431 #endif
432  { _M_t.swap(__x._M_t); }
433 
434  // insert/erase
435 #if __cplusplus >= 201103L
436  /**
437  * @brief Attempts to build and insert an element into the %set.
438  * @param __args Arguments used to generate an element.
439  * @return A pair, of which the first element is an iterator that points
440  * to the possibly inserted element, and the second is a bool
441  * that is true if the element was actually inserted.
442  *
443  * This function attempts to build and insert an element into the %set.
444  * A %set relies on unique keys and thus an element is only inserted if
445  * it is not already present in the %set.
446  *
447  * Insertion requires logarithmic time.
448  */
449  template<typename... _Args>
451  emplace(_Args&&... __args)
452  { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
453 
454  /**
455  * @brief Attempts to insert an element into the %set.
456  * @param __pos An iterator that serves as a hint as to where the
457  * element should be inserted.
458  * @param __args Arguments used to generate the element to be
459  * inserted.
460  * @return An iterator that points to the element with key equivalent to
461  * the one generated from @a __args (may or may not be the
462  * element itself).
463  *
464  * This function is not concerned about whether the insertion took place,
465  * and thus does not return a boolean like the single-argument emplace()
466  * does. Note that the first parameter is only a hint and can
467  * potentially improve the performance of the insertion process. A bad
468  * hint would cause no gains in efficiency.
469  *
470  * For more on @a hinting, see:
471  * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
472  *
473  * Insertion requires logarithmic time (if the hint is not taken).
474  */
475  template<typename... _Args>
476  iterator
477  emplace_hint(const_iterator __pos, _Args&&... __args)
478  {
479  return _M_t._M_emplace_hint_unique(__pos,
480  std::forward<_Args>(__args)...);
481  }
482 #endif
483 
484  /**
485  * @brief Attempts to insert an element into the %set.
486  * @param __x Element to be inserted.
487  * @return A pair, of which the first element is an iterator that points
488  * to the possibly inserted element, and the second is a bool
489  * that is true if the element was actually inserted.
490  *
491  * This function attempts to insert an element into the %set. A %set
492  * relies on unique keys and thus an element is only inserted if it is
493  * not already present in the %set.
494  *
495  * Insertion requires logarithmic time.
496  */
498  insert(const value_type& __x)
499  {
501  _M_t._M_insert_unique(__x);
502  return std::pair<iterator, bool>(__p.first, __p.second);
503  }
504 
505 #if __cplusplus >= 201103L
507  insert(value_type&& __x)
508  {
510  _M_t._M_insert_unique(std::move(__x));
511  return std::pair<iterator, bool>(__p.first, __p.second);
512  }
513 #endif
514 
515  /**
516  * @brief Attempts to insert an element into the %set.
517  * @param __position An iterator that serves as a hint as to where the
518  * element should be inserted.
519  * @param __x Element to be inserted.
520  * @return An iterator that points to the element with key of
521  * @a __x (may or may not be the element passed in).
522  *
523  * This function is not concerned about whether the insertion took place,
524  * and thus does not return a boolean like the single-argument insert()
525  * does. Note that the first parameter is only a hint and can
526  * potentially improve the performance of the insertion process. A bad
527  * hint would cause no gains in efficiency.
528  *
529  * For more on @a hinting, see:
530  * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
531  *
532  * Insertion requires logarithmic time (if the hint is not taken).
533  */
534  iterator
535  insert(const_iterator __position, const value_type& __x)
536  { return _M_t._M_insert_unique_(__position, __x); }
537 
538 #if __cplusplus >= 201103L
539  iterator
540  insert(const_iterator __position, value_type&& __x)
541  { return _M_t._M_insert_unique_(__position, std::move(__x)); }
542 #endif
543 
544  /**
545  * @brief A template function that attempts to insert a range
546  * of elements.
547  * @param __first Iterator pointing to the start of the range to be
548  * inserted.
549  * @param __last Iterator pointing to the end of the range.
550  *
551  * Complexity similar to that of the range constructor.
552  */
553  template<typename _InputIterator>
554  void
555  insert(_InputIterator __first, _InputIterator __last)
556  { _M_t._M_insert_unique(__first, __last); }
557 
558 #if __cplusplus >= 201103L
559  /**
560  * @brief Attempts to insert a list of elements into the %set.
561  * @param __l A std::initializer_list<value_type> of elements
562  * to be inserted.
563  *
564  * Complexity similar to that of the range constructor.
565  */
566  void
568  { this->insert(__l.begin(), __l.end()); }
569 #endif
570 
571 #if __cplusplus >= 201103L
572  // _GLIBCXX_RESOLVE_LIB_DEFECTS
573  // DR 130. Associative erase should return an iterator.
574  /**
575  * @brief Erases an element from a %set.
576  * @param __position An iterator pointing to the element to be erased.
577  * @return An iterator pointing to the element immediately following
578  * @a __position prior to the element being erased. If no such
579  * element exists, end() is returned.
580  *
581  * This function erases an element, pointed to by the given iterator,
582  * from a %set. Note that this function only erases the element, and
583  * that if the element is itself a pointer, the pointed-to memory is not
584  * touched in any way. Managing the pointer is the user's
585  * responsibility.
586  */
587  _GLIBCXX_ABI_TAG_CXX11
588  iterator
589  erase(const_iterator __position)
590  { return _M_t.erase(__position); }
591 #else
592  /**
593  * @brief Erases an element from a %set.
594  * @param position An iterator pointing to the element to be erased.
595  *
596  * This function erases an element, pointed to by the given iterator,
597  * from a %set. Note that this function only erases the element, and
598  * that if the element is itself a pointer, the pointed-to memory is not
599  * touched in any way. Managing the pointer is the user's
600  * responsibility.
601  */
602  void
603  erase(iterator __position)
604  { _M_t.erase(__position); }
605 #endif
606 
607  /**
608  * @brief Erases elements according to the provided key.
609  * @param __x Key of element to be erased.
610  * @return The number of elements erased.
611  *
612  * This function erases all the elements located by the given key from
613  * a %set.
614  * Note that this function only erases the element, and that if
615  * the element is itself a pointer, the pointed-to memory is not touched
616  * in any way. Managing the pointer is the user's responsibility.
617  */
618  size_type
619  erase(const key_type& __x)
620  { return _M_t.erase(__x); }
621 
622 #if __cplusplus >= 201103L
623  // _GLIBCXX_RESOLVE_LIB_DEFECTS
624  // DR 130. Associative erase should return an iterator.
625  /**
626  * @brief Erases a [__first,__last) range of elements from a %set.
627  * @param __first Iterator pointing to the start of the range to be
628  * erased.
629 
630  * @param __last Iterator pointing to the end of the range to
631  * be erased.
632  * @return The iterator @a __last.
633  *
634  * This function erases a sequence of elements from a %set.
635  * Note that this function only erases the element, and that if
636  * the element is itself a pointer, the pointed-to memory is not touched
637  * in any way. Managing the pointer is the user's responsibility.
638  */
639  _GLIBCXX_ABI_TAG_CXX11
640  iterator
642  { return _M_t.erase(__first, __last); }
643 #else
644  /**
645  * @brief Erases a [first,last) range of elements from a %set.
646  * @param __first Iterator pointing to the start of the range to be
647  * erased.
648  * @param __last Iterator pointing to the end of the range to
649  * be erased.
650  *
651  * This function erases a sequence of elements from a %set.
652  * Note that this function only erases the element, and that if
653  * the element is itself a pointer, the pointed-to memory is not touched
654  * in any way. Managing the pointer is the user's responsibility.
655  */
656  void
657  erase(iterator __first, iterator __last)
658  { _M_t.erase(__first, __last); }
659 #endif
660 
661  /**
662  * Erases all elements in a %set. Note that this function only erases
663  * the elements, and that if the elements themselves are pointers, the
664  * pointed-to memory is not touched in any way. Managing the pointer is
665  * the user's responsibility.
666  */
667  void
668  clear() _GLIBCXX_NOEXCEPT
669  { _M_t.clear(); }
670 
671  // set operations:
672 
673  /**
674  * @brief Finds the number of elements.
675  * @param __x Element to located.
676  * @return Number of elements with specified key.
677  *
678  * This function only makes sense for multisets; for set the result will
679  * either be 0 (not present) or 1 (present).
680  */
681  size_type
682  count(const key_type& __x) const
683  { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
684 
685  // _GLIBCXX_RESOLVE_LIB_DEFECTS
686  // 214. set::find() missing const overload
687  //@{
688  /**
689  * @brief Tries to locate an element in a %set.
690  * @param __x Element to be located.
691  * @return Iterator pointing to sought-after element, or end() if not
692  * found.
693  *
694  * This function takes a key and tries to locate the element with which
695  * the key matches. If successful the function returns an iterator
696  * pointing to the sought after element. If unsuccessful it returns the
697  * past-the-end ( @c end() ) iterator.
698  */
699  iterator
700  find(const key_type& __x)
701  { return _M_t.find(__x); }
702 
704  find(const key_type& __x) const
705  { return _M_t.find(__x); }
706  //@}
707 
708  //@{
709  /**
710  * @brief Finds the beginning of a subsequence matching given key.
711  * @param __x Key to be located.
712  * @return Iterator pointing to first element equal to or greater
713  * than key, or end().
714  *
715  * This function returns the first element of a subsequence of elements
716  * that matches the given key. If unsuccessful it returns an iterator
717  * pointing to the first element that has a greater value than given key
718  * or end() if no such element exists.
719  */
720  iterator
721  lower_bound(const key_type& __x)
722  { return _M_t.lower_bound(__x); }
723 
725  lower_bound(const key_type& __x) const
726  { return _M_t.lower_bound(__x); }
727  //@}
728 
729  //@{
730  /**
731  * @brief Finds the end of a subsequence matching given key.
732  * @param __x Key to be located.
733  * @return Iterator pointing to the first element
734  * greater than key, or end().
735  */
736  iterator
737  upper_bound(const key_type& __x)
738  { return _M_t.upper_bound(__x); }
739 
741  upper_bound(const key_type& __x) const
742  { return _M_t.upper_bound(__x); }
743  //@}
744 
745  //@{
746  /**
747  * @brief Finds a subsequence matching given key.
748  * @param __x Key to be located.
749  * @return Pair of iterators that possibly points to the subsequence
750  * matching given key.
751  *
752  * This function is equivalent to
753  * @code
754  * std::make_pair(c.lower_bound(val),
755  * c.upper_bound(val))
756  * @endcode
757  * (but is faster than making the calls separately).
758  *
759  * This function probably only makes sense for multisets.
760  */
762  equal_range(const key_type& __x)
763  { return _M_t.equal_range(__x); }
764 
766  equal_range(const key_type& __x) const
767  { return _M_t.equal_range(__x); }
768  //@}
769 
770  template<typename _K1, typename _C1, typename _A1>
771  friend bool
772  operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
773 
774  template<typename _K1, typename _C1, typename _A1>
775  friend bool
776  operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
777  };
778 
779 
780  /**
781  * @brief Set equality comparison.
782  * @param __x A %set.
783  * @param __y A %set of the same type as @a x.
784  * @return True iff the size and elements of the sets are equal.
785  *
786  * This is an equivalence relation. It is linear in the size of the sets.
787  * Sets are considered equivalent if their sizes are equal, and if
788  * corresponding elements compare equal.
789  */
790  template<typename _Key, typename _Compare, typename _Alloc>
791  inline bool
792  operator==(const set<_Key, _Compare, _Alloc>& __x,
793  const set<_Key, _Compare, _Alloc>& __y)
794  { return __x._M_t == __y._M_t; }
795 
796  /**
797  * @brief Set ordering relation.
798  * @param __x A %set.
799  * @param __y A %set of the same type as @a x.
800  * @return True iff @a __x is lexicographically less than @a __y.
801  *
802  * This is a total ordering relation. It is linear in the size of the
803  * sets. The elements must be comparable with @c <.
804  *
805  * See std::lexicographical_compare() for how the determination is made.
806  */
807  template<typename _Key, typename _Compare, typename _Alloc>
808  inline bool
809  operator<(const set<_Key, _Compare, _Alloc>& __x,
810  const set<_Key, _Compare, _Alloc>& __y)
811  { return __x._M_t < __y._M_t; }
812 
813  /// Returns !(x == y).
814  template<typename _Key, typename _Compare, typename _Alloc>
815  inline bool
816  operator!=(const set<_Key, _Compare, _Alloc>& __x,
817  const set<_Key, _Compare, _Alloc>& __y)
818  { return !(__x == __y); }
819 
820  /// Returns y < x.
821  template<typename _Key, typename _Compare, typename _Alloc>
822  inline bool
823  operator>(const set<_Key, _Compare, _Alloc>& __x,
824  const set<_Key, _Compare, _Alloc>& __y)
825  { return __y < __x; }
826 
827  /// Returns !(y < x)
828  template<typename _Key, typename _Compare, typename _Alloc>
829  inline bool
830  operator<=(const set<_Key, _Compare, _Alloc>& __x,
831  const set<_Key, _Compare, _Alloc>& __y)
832  { return !(__y < __x); }
833 
834  /// Returns !(x < y)
835  template<typename _Key, typename _Compare, typename _Alloc>
836  inline bool
837  operator>=(const set<_Key, _Compare, _Alloc>& __x,
838  const set<_Key, _Compare, _Alloc>& __y)
839  { return !(__x < __y); }
840 
841  /// See std::set::swap().
842  template<typename _Key, typename _Compare, typename _Alloc>
843  inline void
845  { __x.swap(__y); }
846 
847 _GLIBCXX_END_NAMESPACE_CONTAINER
848 } //namespace std
849 #endif /* _STL_SET_H */
_Alloc_traits::pointer pointer
Iterator-related typedefs.
Definition: stl_set.h:123
set(const allocator_type &__a)
Allocator-extended default constructor.
Definition: stl_set.h:229
_Alloc_traits::const_pointer const_pointer
Iterator-related typedefs.
Definition: stl_set.h:124
const_iterator find(const key_type &__x) const
Tries to locate an element in a set.
Definition: stl_set.h:704
iterator insert(const_iterator __position, const value_type &__x)
Attempts to insert an element into the set.
Definition: stl_set.h:535
_GLIBCXX_ABI_TAG_CXX11 iterator erase(const_iterator __position)
Erases an element from a set.
Definition: stl_set.h:589
set(const set &__x, const allocator_type &__a)
Allocator-extended copy constructor.
Definition: stl_set.h:233
_Key key_type
Public typedefs.
Definition: stl_set.h:103
size_type count(const key_type &__x) const
Finds the number of elements.
Definition: stl_set.h:682
_T2 second
first is a copy of the first object
Definition: stl_pair.h:102
set()
Default constructor creates no elements.
Definition: stl_set.h:142
set(const _Compare &__comp, const allocator_type &__a=allocator_type())
Creates a set with no elements.
Definition: stl_set.h:151
Uniform interface to C++98 and C++0x allocators.
set & operator=(const set &__x)
Set assignment operator.
Definition: stl_set.h:263
reverse_iterator crend() const noexcept
Definition: stl_set.h:397
reverse_iterator rbegin() const noexcept
Definition: stl_set.h:351
void clear() noexcept
Definition: stl_set.h:668
set & operator=(initializer_list< value_type > __l)
Set list assignment operator.
Definition: stl_set.h:304
_Rep_type::const_iterator iterator
Iterator-related typedefs.
Definition: stl_set.h:130
_Compare value_compare
Public typedefs.
Definition: stl_set.h:106
_T1 first
second_type is the second bound type
Definition: stl_pair.h:101
size_type max_size() const noexcept
Returns the maximum size of the set.
Definition: stl_set.h:413
_Compare key_compare
Public typedefs.
Definition: stl_set.h:105
_Alloc allocator_type
Public typedefs.
Definition: stl_set.h:107
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:96
_Rep_type::const_iterator const_iterator
Iterator-related typedefs.
Definition: stl_set.h:131
noexcept(is_nothrow_copy_constructible< _Compare >::value)
Set move constructor
Definition: stl_set.h:208
iterator find(const key_type &__x)
Tries to locate an element in a set.
Definition: stl_set.h:700
_Alloc_traits::reference reference
Iterator-related typedefs.
Definition: stl_set.h:125
std::pair< iterator, bool > emplace(_Args &&...__args)
Attempts to build and insert an element into the set.
Definition: stl_set.h:451
set(initializer_list< value_type > __l, const _Compare &__comp=_Compare(), const allocator_type &__a=allocator_type())
Builds a set from an initializer_list.
Definition: stl_set.h:221
bool empty() const noexcept
Returns true if the set is empty.
Definition: stl_set.h:403
iterator cend() const noexcept
Definition: stl_set.h:379
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition: stl_set.h:619
size_type size() const noexcept
Returns the size of the set.
Definition: stl_set.h:408
reverse_iterator rend() const noexcept
Definition: stl_set.h:360
set(initializer_list< value_type > __l, const allocator_type &__a)
Allocator-extended initialier-list constructor.
Definition: stl_set.h:243
key_compare key_comp() const
Returns the comparison object with which the set was constructed.
Definition: stl_set.h:316
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition: stl_set.h:737
std::pair< iterator, bool > insert(const value_type &__x)
Attempts to insert an element into the set.
Definition: stl_set.h:498
set(_InputIterator __first, _InputIterator __last)
Builds a set from a range.
Definition: stl_set.h:166
iterator cbegin() const noexcept
Definition: stl_set.h:370
allocator_type get_allocator() const noexcept
Returns the allocator object with which the set was constructed.
Definition: stl_set.h:324
_Alloc_traits::const_reference const_reference
Iterator-related typedefs.
Definition: stl_set.h:126
set(_InputIterator __first, _InputIterator __last, const allocator_type &__a)
Allocator-extended range constructor.
Definition: stl_set.h:249
_Rep_type::difference_type difference_type
Iterator-related typedefs.
Definition: stl_set.h:135
_Rep_type::const_reverse_iterator const_reverse_iterator
Iterator-related typedefs.
Definition: stl_set.h:133
set(_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type())
Builds a set from a range.
Definition: stl_set.h:183
iterator begin() const noexcept
Definition: stl_set.h:333
set(const set &__x)
Set copy constructor.
Definition: stl_set.h:196
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition: stl_set.h:741
_Key value_type
Public typedefs.
Definition: stl_set.h:104
_Rep_type::const_reverse_iterator reverse_iterator
Iterator-related typedefs.
Definition: stl_set.h:132
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition: stl_set.h:721
iterator end() const noexcept
Definition: stl_set.h:342
reverse_iterator crbegin() const noexcept
Definition: stl_set.h:388
The standard allocator, as per [20.4].
Definition: allocator.h:92
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
Definition: functional:2534
Common iterator class.
initializer_list
value_compare value_comp() const
Returns the comparison object with which the set was constructed.
Definition: stl_set.h:320
iterator emplace_hint(const_iterator __pos, _Args &&...__args)
Attempts to insert an element into the set.
Definition: stl_set.h:477
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition: stl_set.h:725
set & operator=(set &&__x) noexcept(_Alloc_traits::_S_nothrow_move())
Set move assignment operator.
Definition: stl_set.h:278
_GLIBCXX_ABI_TAG_CXX11 iterator erase(const_iterator __first, const_iterator __last)
Erases a [__first,__last) range of elements from a set.
Definition: stl_set.h:641
void insert(_InputIterator __first, _InputIterator __last)
A template function that attempts to insert a range of elements.
Definition: stl_set.h:555
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition: stl_set.h:766
_Rep_type::size_type size_type
Iterator-related typedefs.
Definition: stl_set.h:134
A standard container made up of unique keys, which can be retrieved in logarithmic time...
Definition: stl_set.h:90
void insert(initializer_list< value_type > __l)
Attempts to insert a list of elements into the set.
Definition: stl_set.h:567
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition: stl_set.h:762