# std::set< Key, Compare, Alloc > Class Template Reference
[Containers, Associative Containers]

A standard container made up of unique keys, which can be retrieved in logarithmic time. More...

`#include <stl_set.h>`

Inheritance diagram for std::set< Key, Compare, Alloc >:

[legend]
List of all members.

## Public Types

typedef Key | key_type
| Public typedefs.
typedef Key | value_type
| Public typedefs.
typedef Compare | key_compare
| Public typedefs.
typedef Compare | value_compare
| Public typedefs.
typedef Alloc::pointer | pointer
| Iterator-related typedefs.
typedef Alloc::const_pointer | const_pointer
| Iterator-related typedefs.
typedef Alloc::reference | reference
| Iterator-related typedefs.
typedef Alloc::const_reference | const_reference
| Iterator-related typedefs.
typedef Rep_type::const_iterator | iterator
| Iterator-related typedefs.
typedef Rep_type::const_iterator | const_iterator
| Iterator-related typedefs.
typedef Rep_type::const_reverse_iterator | reverse_iterator
| Iterator-related typedefs.
typedef Rep_type::const_reverse_iterator | const_reverse_iterator
| Iterator-related typedefs.
typedef Rep_type::size_type | size_type
| Iterator-related typedefs.
typedef Rep_type::difference_type | difference_type
| Iterator-related typedefs.
typedef Rep_type::allocator_type | allocator_type
| Iterator-related typedefs.

## Public Member Functions

| set ()
| Default constructor creates no elements.
| set (const Compare &comp, const allocator_type &a=allocator_type())
| Default constructor creates no elements.
template<class InputIterator> | set (InputIterator first, InputIterator last)
| Builds a set from a range.
template<class InputIterator> | set (InputIterator first, InputIterator last, const Compare &comp, const allocator_type &a=allocator_type())
| Builds a set from a range.
| set (const set< Key, Compare, Alloc > &x)
| Set copy constructor.
set< Key, Compare, Alloc > & | operator= (const set< Key, Compare, Alloc > &x)
| Set assignment operator.
key_compare | key_comp () const
| Returns the comparison object with which the set was constructed.
value_compare | value_comp () const
| Returns the comparison object with which the set was constructed.
allocator_type | get_allocator () const
| Returns the allocator object with which the set was constructed.
iterator | begin () const
iterator | end () const
reverse_iterator | rbegin () const
reverse_iterator | rend () const
bool | empty () const
| Returns true if the set is empty.
size_type | size () const
| Returns the size of the set.
size_type | max_size () const
| Returns the maximum size of the set.
void | swap (set< Key, Compare, Alloc > &x)
| Swaps data with another set.
pair< iterator, bool > | insert (const value_type &x)
| Attempts to insert an element into the set.
iterator | insert (iterator position, const value_type &x)
| Attempts to insert an element into the set.
template<class InputIterator> void | insert (InputIterator first, InputIterator last)
| A template function that attemps to insert a range of elements.
void | erase (iterator position)
| Erases an element from a set.
size_type | erase (const key_type &x)
| Erases elements according to the provided key.
void | erase (iterator first, iterator last)
| Erases a [first,last) range of elements from a set.
void | clear ()
size_type | count (const key_type &x) const
| Finds the number of elements.
iterator | find (const key_type &x)
| Tries to locate an element in a set.
const_iterator | find (const key_type &x) const
| Tries to locate an element in a set.
iterator | lower_bound (const key_type &x)
| Finds the beginning of a subsequence matching given key.
const_iterator | lower_bound (const key_type &x) const
| Finds the beginning of a subsequence matching given key.
iterator | upper_bound (const key_type &x)
| Finds the end of a subsequence matching given key.
const_iterator | upper_bound (const key_type &x) const
| Finds the end of a subsequence matching given key.
pair< iterator, iterator > | equal_range (const key_type &x)
| Finds a subsequence matching given key.
pair< const_iterator, const_iterator > | equal_range (const key_type &x) const
| Finds a subsequence matching given key.

## Detailed Description

### template<class Key, class Compare, class Alloc>
class std::set< Key, Compare, Alloc >

A standard container made up of unique keys, which can be retrieved in logarithmic time.

Meets the requirements of a container, a reversible container, and an associative container (using unique keys).

Sets support bidirectional iterators.

Parameters:

  | Key | Type of key objects.
  | Compare | Comparison function object type, defaults to less<Key>.
  | Alloc | Allocator type, defaults to allocator<Key>.

Definition at line 107 of file stl_set.h.

## Member Typedef Documentation

template<class Key, class Compare, class Alloc>
typedef Rep_type::allocator_type std::set< Key, Compare, Alloc >::allocator_type

| Iterator-related typedefs. Definition at line 144 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Rep_type::const_iterator std::set< Key, Compare, Alloc >::const_iterator

| Iterator-related typedefs. Definition at line 139 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Alloc::const_pointer std::set< Key, Compare, Alloc >::const_pointer

| Iterator-related typedefs. Definition at line 132 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Alloc::const_reference std::set< Key, Compare, Alloc >::const_reference

| Iterator-related typedefs. Definition at line 134 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Rep_type::const_reverse_iterator std::set< Key, Compare, Alloc >::const_reverse_iterator

| Iterator-related typedefs. Definition at line 141 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Rep_type::difference_type std::set< Key, Compare, Alloc >::difference_type

| Iterator-related typedefs. Definition at line 143 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Rep_type::const_iterator std::set< Key, Compare, Alloc >::iterator

| Iterator-related typedefs. Definition at line 138 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Compare std::set< Key, Compare, Alloc >::key_compare

| Public typedefs. Definition at line 120 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Key std::set< Key, Compare, Alloc >::key_type

| Public typedefs. Definition at line 118 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Alloc::pointer std::set< Key, Compare, Alloc >::pointer

| Iterator-related typedefs. Definition at line 131 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Alloc::reference std::set< Key, Compare, Alloc >::reference

| Iterator-related typedefs. Definition at line 133 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Rep_type::const_reverse_iterator std::set< Key, Compare, Alloc >::reverse_iterator

| Iterator-related typedefs. Definition at line 140 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Rep_type::size_type std::set< Key, Compare, Alloc >::size_type

| Iterator-related typedefs. Definition at line 142 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Compare std::set< Key, Compare, Alloc >::value_compare

| Public typedefs. Definition at line 121 of file stl_set.h.

template<class Key, class Compare, class Alloc>
typedef Key std::set< Key, Compare, Alloc >::value_type

| Public typedefs. Definition at line 119 of file stl_set.h.

## Constructor & Destructor Documentation

template<class Key, class Compare, class Alloc>
std::set< Key, Compare, Alloc >::set | ( |  | ) | `[inline]`

| Default constructor creates no elements. Definition at line 149 of file stl_set.h.

template<class Key, class Compare, class Alloc>
std::set< Key, Compare, Alloc >::set | ( | const Compare & | comp,
|  | const allocator_type & | a = `allocator_type()`
| ) | `[inline, explicit]`

Default constructor creates no elements.

Parameters:

  | comp | Comparator to use.
  | a | Allocator to use.

Definition at line 158 of file stl_set.h.

template<class Key, class Compare, class Alloc>
template<class InputIterator>
std::set< Key, Compare, Alloc >::set | ( | InputIterator | first,
|  | InputIterator | last
| ) | `[inline]`

Builds a set from a range.

Parameters:

  | first | An input iterator.
  | last | An input iterator.

Create a set consisting of copies of the elements from [first,last). This is linear in N if the range is already sorted, and NlogN otherwise (where N is distance(first,last)).

Definition at line 172 of file stl_set.h.

template<class Key, class Compare, class Alloc>
template<class InputIterator>
std::set< Key, Compare, Alloc >::set | ( | InputIterator | first,
|  | InputIterator | last,
|  | const Compare & | comp,
|  | const allocator_type & | a = `allocator_type()`
| ) | `[inline]`

Builds a set from a range.

Parameters:

  | first | An input iterator.
  | last | An input iterator.
  | comp | A comparison functor.
  | a | An allocator object.

Create a set consisting of copies of the elements from [first,last). This is linear in N if the range is already sorted, and NlogN otherwise (where N is distance(first,last)).

Definition at line 188 of file stl_set.h.

template<class Key, class Compare, class Alloc>
std::set< Key, Compare, Alloc >::set | ( | const set< Key, Compare, Alloc > & | x | ) | `[inline]`

Set copy constructor.

Parameters:

  | x | A set of identical element and allocator types.

The newly-created set uses a copy of the allocation object used by x.

Definition at line 201 of file stl_set.h.

## Member Function Documentation

template<class Key, class Compare, class Alloc>
iterator std::set< Key, Compare, Alloc >::begin | ( |  | ) | const`[inline]`

| Returns a read/write iterator that points to the first element in the set. Iteration is done in ascending order according to the keys. Definition at line 238 of file stl_set.h.

template<class Key, class Compare, class Alloc>
void std::set< Key, Compare, Alloc >::clear | ( |  | ) | `[inline]`

| Erases all elements in a set. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty. Definition at line 412 of file stl_set.h.

template<class Key, class Compare, class Alloc>
size_type std::set< Key, Compare, Alloc >::count | ( | const key_type & | x | ) | const`[inline]`

Finds the number of elements.

Parameters:

  | x | Element to located.

Returns:
  Number of elements with specified key.

This function only makes sense for multisets; for set the result will either be 0 (not present) or 1 (present).

Definition at line 426 of file stl_set.h.

template<class Key, class Compare, class Alloc>
bool std::set< Key, Compare, Alloc >::empty | ( |  | ) | const`[inline]`

| Returns true if the set is empty. Definition at line 269 of file stl_set.h.

template<class Key, class Compare, class Alloc>
iterator std::set< Key, Compare, Alloc >::end | ( |  | ) | const`[inline]`

| Returns a read/write iterator that points one past the last element in the set. Iteration is done in ascending order according to the keys. Definition at line 246 of file stl_set.h.

template<class Key, class Compare, class Alloc>
pair<const_iterator,const_iterator> std::set< Key, Compare, Alloc >::equal_range | ( | const key_type & | x | ) | const`[inline]`

Finds a subsequence matching given key.

Parameters:

  | x | Key to be located.

Returns:
  Pair of iterators that possibly points to the subsequence matching given key.

This function is equivalent to

        std::make_pair(c.lower_bound(val),
                       c.upper_bound(val))

(but is faster than making the calls separately).

This function probably only makes sense for multisets.

Definition at line 510 of file stl_set.h.

template<class Key, class Compare, class Alloc>
pair<iterator,iterator> std::set< Key, Compare, Alloc >::equal_range | ( | const key_type & | x | ) | `[inline]`

Finds a subsequence matching given key.

Parameters:

  | x | Key to be located.

Returns:
  Pair of iterators that possibly points to the subsequence matching given key.

This function is equivalent to

        std::make_pair(c.lower_bound(val),
                       c.upper_bound(val))

(but is faster than making the calls separately).

This function probably only makes sense for multisets.

Definition at line 506 of file stl_set.h.

template<class Key, class Compare, class Alloc>
void std::set< Key, Compare, Alloc >::erase | ( | iterator | first,
|  | iterator | last
| ) | `[inline]`

Erases a [first,last) range of elements from a set.

Parameters:

  | first | Iterator pointing to the start of the range to be erased.
  | last | Iterator pointing to the end of the range to be erased.

This function erases a sequence of elements from a set. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty.

Definition at line 399 of file stl_set.h.

template<class Key, class Compare, class Alloc>
size_type std::set< Key, Compare, Alloc >::erase | ( | const key_type & | x | ) | `[inline]`

Erases elements according to the provided key.

Parameters:

  | x | Key of element to be erased.

Returns:
  The number of elements erased.

This function erases all the elements located by the given key from a set. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty.

Definition at line 385 of file stl_set.h.

template<class Key, class Compare, class Alloc>
void std::set< Key, Compare, Alloc >::erase | ( | iterator | position | ) | `[inline]`

Erases an element from a set.

Parameters:

  | position | An iterator pointing to the element to be erased.

This function erases an element, pointed to by the given iterator, from a set. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibilty.

Definition at line 367 of file stl_set.h.

template<class Key, class Compare, class Alloc>
const_iterator std::set< Key, Compare, Alloc >::find | ( | const key_type & | x | ) | const`[inline]`

Tries to locate an element in a set.

Parameters:

  | x | Element to be located.

Returns:
  Iterator pointing to sought-after element, or end() if not found.

This function takes a key and tries to locate the element with which the key matches. If successful the function returns an iterator pointing to the sought after element. If unsuccessful it returns the past-the-end ( `end()` ) iterator.

Definition at line 448 of file stl_set.h.

template<class Key, class Compare, class Alloc>
iterator std::set< Key, Compare, Alloc >::find | ( | const key_type & | x | ) | `[inline]`

Tries to locate an element in a set.

Parameters:

  | x | Element to be located.

Returns:
  Iterator pointing to sought-after element, or end() if not found.

This function takes a key and tries to locate the element with which the key matches. If successful the function returns an iterator pointing to the sought after element. If unsuccessful it returns the past-the-end ( `end()` ) iterator.

Definition at line 444 of file stl_set.h.

template<class Key, class Compare, class Alloc>
allocator_type std::set< Key, Compare, Alloc >::get_allocator | ( |  | ) | const`[inline]`

| Returns the allocator object with which the set was constructed. Definition at line 230 of file stl_set.h.

template<class Key, class Compare, class Alloc>
template<class InputIterator>
void std::set< Key, Compare, Alloc >::insert | ( | InputIterator | first,
|  | InputIterator | last
| ) | `[inline]`

A template function that attemps to insert a range of elements.

Parameters:

  | first | Iterator pointing to the start of the range to be inserted.
  | last | Iterator pointing to the end of the range.

Complexity similar to that of the range constructor.

Definition at line 354 of file stl_set.h.

template<class Key, class Compare, class Alloc>
iterator std::set< Key, Compare, Alloc >::insert | ( | iterator | position,
|  | const value_type & | x
| ) | `[inline]`

Attempts to insert an element into the set.

Parameters:

  | position | An iterator that serves as a hint as to where the element should be inserted.
  | x | Element to be inserted.

Returns:
  An iterator that points to the element with key of x (may or may not be the element passed in).

This function is not concerned about whether the insertion took place, and thus does not return a boolean like the single-argument insert() does. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency.

See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4 for more on "hinting".

Insertion requires logarithmic time (if the hint is not taken).

Definition at line 338 of file stl_set.h.

template<class Key, class Compare, class Alloc>
pair<iterator,bool> std::set< Key, Compare, Alloc >::insert | ( | const value_type & | x | ) | `[inline]`

Attempts to insert an element into the set.

Parameters:

  | x | Element to be inserted.

Returns:
  A pair, of which the first element is an iterator that points to the possibly inserted element, and the second is a bool that is true if the element was actually inserted.

This function attempts to insert an element into the set. A set relies on unique keys and thus an element is only inserted if it is not already present in the set.

Insertion requires logarithmic time.

Definition at line 312 of file stl_set.h.

template<class Key, class Compare, class Alloc>
key_compare std::set< Key, Compare, Alloc >::key_comp | ( |  | ) | const`[inline]`

| Returns the comparison object with which the set was constructed. Definition at line 222 of file stl_set.h. Referenced by std::set< Key, Compare, Allocator >::key_comp().

template<class Key, class Compare, class Alloc>
const_iterator std::set< Key, Compare, Alloc >::lower_bound | ( | const key_type & | x | ) | const`[inline]`

Finds the beginning of a subsequence matching given key.

Parameters:

  | x | Key to be located.

Returns:
  Iterator pointing to first element equal to or greater than key, or end().

This function returns the first element of a subsequence of elements that matches the given key. If unsuccessful it returns an iterator pointing to the first element that has a greater value than given key or end() if no such element exists.

Definition at line 469 of file stl_set.h.

template<class Key, class Compare, class Alloc>
iterator std::set< Key, Compare, Alloc >::lower_bound | ( | const key_type & | x | ) | `[inline]`

Finds the beginning of a subsequence matching given key.

Parameters:

  | x | Key to be located.

Returns:
  Iterator pointing to first element equal to or greater than key, or end().

This function returns the first element of a subsequence of elements that matches the given key. If unsuccessful it returns an iterator pointing to the first element that has a greater value than given key or end() if no such element exists.

Definition at line 465 of file stl_set.h.

template<class Key, class Compare, class Alloc>
size_type std::set< Key, Compare, Alloc >::max_size | ( |  | ) | const`[inline]`

| Returns the maximum size of the set. Definition at line 279 of file stl_set.h.

template<class Key, class Compare, class Alloc>
set<Key,Compare,Alloc>& std::set< Key, Compare, Alloc >::operator= | ( | const set< Key, Compare, Alloc > & | x | ) | `[inline]`

Set assignment operator.

Parameters:

  | x | A set of identical element and allocator types.

All the elements of x are copied, but unlike the copy constructor, the allocator object is not copied.

Definition at line 212 of file stl_set.h.

template<class Key, class Compare, class Alloc>
reverse_iterator std::set< Key, Compare, Alloc >::rbegin | ( |  | ) | const`[inline]`

| Returns a read/write reverse iterator that points to the last element in the set. Iteration is done in descending order according to the keys. Definition at line 255 of file stl_set.h.

template<class Key, class Compare, class Alloc>
reverse_iterator std::set< Key, Compare, Alloc >::rend | ( |  | ) | const`[inline]`

| Returns a read-only (constant) reverse iterator that points to the last pair in the map. Iteration is done in descending order according to the keys. Definition at line 264 of file stl_set.h.

template<class Key, class Compare, class Alloc>
size_type std::set< Key, Compare, Alloc >::size | ( |  | ) | const`[inline]`

| Returns the size of the set. Definition at line 274 of file stl_set.h.

template<class Key, class Compare, class Alloc>
void std::set< Key, Compare, Alloc >::swap | ( | set< Key, Compare, Alloc > & | x | ) | `[inline]`

Swaps data with another set.

Parameters:

  | x | A set of the same element and allocator types.

This exchanges the elements between two sets in constant time. (It is only swapping a pointer, an integer, and an instance of the `Compare` type (which itself is often stateless and empty), so it should be quite fast.) Note that the global std::swap() function is specialized such that std::swap(s1,s2) will feed to this function.

Definition at line 294 of file stl_set.h.

Referenced by std::swap().

template<class Key, class Compare, class Alloc>
const_iterator std::set< Key, Compare, Alloc >::upper_bound | ( | const key_type & | x | ) | const`[inline]`

Finds the end of a subsequence matching given key.

Parameters:

  | x | Key to be located.

Returns:
  Iterator pointing to the first element greater than key, or end().

Definition at line 485 of file stl_set.h.

template<class Key, class Compare, class Alloc>
iterator std::set< Key, Compare, Alloc >::upper_bound | ( | const key_type & | x | ) | `[inline]`

Finds the end of a subsequence matching given key.

Parameters:

  | x | Key to be located.

Returns:
  Iterator pointing to the first element greater than key, or end().

Definition at line 481 of file stl_set.h.

template<class Key, class Compare, class Alloc>
value_compare std::set< Key, Compare, Alloc >::value_comp | ( |  | ) | const`[inline]`

| Returns the comparison object with which the set was constructed. Definition at line 226 of file stl_set.h.

The documentation for this class was generated from the following file:

- stl_set.h