set<T>

The STL set represents an arbitrary collection of elements that can be queried for membership. set is useful in situations where you need to keep track of a collection of elements and must rapidly look up whether an element is contained.

The set does not store duplicates. For a similar container where duplicates are allowed, use the multiset.

By default, the set uses operator < to order the elements behind the scenes. To use an alternative comparison function, parametrize the set as set<T, CompareFunctorType>.

Common set<T> Member Functions
Constructor: set<T> ()
Constructs a new empty set.
set<int> mySet; // Constructs a new, empty set.

Constructs a new set. This is the default constructor.

pair<iterator, bool> insert(const T &elem)
void insert(InputIterator start, InputIterator stop)
Insert elements pairs
mySet.insert(137); // Insert number 137
mySet.insert(myOtherContainer.begin(), myOtherContainer.end()); // Insert elements from another iterator range.

Inserts new elements into the set. The first version of insert inserts a single element into the set. insert returns a pair<iterator, bool>, where the first field of the pair is an iterator to the element and the second field is a bool representing whether the element was inserted. If the element already existed, this value will be false; if the element was created for the first time, it will be true.

The second version of this function inserts a range of elements into the set, removing duplicates.

iterator find(const T &elem)
const_iterator find(const T &elem) const
Return iterator to element
if(mySet.find(137) == mySet.end()) // 137 is not contained in the set

Returns an iterator to the specified element, if it exists, and the result of the set's end function otherwise.

size_type count(const T &elem) const
Determine membership
if(mySet.count(137)) // 137 is contained in the set

Returns 1 if the specified element is contained in the set and 0 otherwise. count is a useful function, but in practice it's far more common to see find.

size_type erase(const T &elem)
void erase(iterator elem)
void erase(iterator start, iterator stop)
Remove elements from the set
mySet.erase(137); // Remove 137 from the set
mySet.erase(myItr); // Erase the element pointed at by myItr
mySet.erase(mySet.begin(), mySet.end()); // Remove all elements from the container

Removes elements from the set. The first version removes the specified element from the set, returning 1 if the element existed and was removed and 0 otherwise. The second version removes from the set the element pointed at by the specified iterator. The third version removes all elements in the range [start, stop) from the set.

bool empty() const
Is container empty?
if(mySet.empty()) { ... }

Returns whether the set is empty. It's considered good practice to call empty to determine whether the set is empty rather than checking whether size() returns zero.

void clear()
Empty container
mySet.clear();

Removes all elements from the set.

size_type size() const
Get container size
int numElems = mySet.size();

Returns the number of elements stored in the set.

iterator begin()
const_iterator begin() const
Get start iterator.
for(set<int>::iterator itr = mySet.begin(); /* ... */)

Returns an iterator to the first element in the set. Elements in a set are stored in sorted order, so the elements returned by iterating over the set will be sorted.

iterator end()
const_iterator end() const
Get stop iterator.
for(/* ... */ itr != mySet.end(); /* ... */)

Returns an iterator to the element one past the end of the set. This iterator does not point to a valid element, so do not dereference it.

Other set<T> Member Functions
Constructor: set<T> (InputIterator begin, InputIterator end)
Constructs a pre-initialized set
set<int> mySet(otherContainer.begin(), otherContainer.end()); // Copy elements from the range otherContainer.begin(), otherContainer.end() into this set

Constructs a new set whose elements are equal to the elements defined in the range [begin, end). These iterators need not come from another set.

iterator lower_bound(const T &value)
const_iterator lower_bound(const T &value) const
Get first element greater than or equal to
set<int>::iterator itr = mySet.lower_bound(137); // Get iterator to first element greater than or equal to 137

Returns an iterator to the first element greater than or equal to the parameter. If no elements are found, lower_bound returns end().

iterator upper_bound(const T &value)
const_iterator upper_bound(const T &value) const
Get first element strictly greater than
set<int>::iterator itr = mySet.upper_bound(137); // Get iterator to first element strictly greater than 137

Returns an iterator to the first element that compares strictly greater than the parameter.

pair<iterator, iterator> equal_range(const T &value)
pair<const_iterator, const_iterator> equal_range(const T &value) const
Get two iterators defining the range of a single element
pair<set<int>::iterator, set<int>::iterator> equal = mySet.equal_range(137);

Returns an pair of iterators defining the range containing the single element specified as a parameter. If the element does not exist, the two iterators will be equal.

size_type max_size() const
Get maximum container size
set<int>::size_type maxElems = mySet.max_size()

Returns the maximum number of elements the set can hold, which is usually the number of unique memory addresses divided by the size of the elements stored in the container.

BinaryFunction key_comp() const
Get element comparison function
if(mySet.keyComp()(myValue1, myValue2)) // Value myValue1 compares less than myValue2

Returns the set's internal comparison function used to compare two elements. You can use this function to compare two elements in the same way as the set.

BinaryFunction value_comp() const
Get element comparison function
if(mySet.keyComp()(myValue1, myValue2)) // Value myValue1 compares less than myValue2

This function is identical to key_comp.

reverse_iterator begin()
const_reverse_iterator begin() const
Get start reverse iterator
for(set<int>::reverse_iterator itr = mySet.rbegin(); /* ... */)

Returns a reverse_iterator to the last element of the set. reverse_iterators are like regular iterators except that they traverse in the opposite direction. Check for the end-of-container condition of reverse_iterators with rend.

reverse_iterator end()
const_reverse_iterator end() const
Get stop reverse iterator.
for(/* ... */ itr != mySet.rend(); /* ... */)

Returns a reverse_iterator to the element one element before the start of the array. Like end, the iterator returned by rend does not actually reference a real element of the set and you should take care not to actually dereference it.

void swap(set<T> &other)
Exchange two setss
mySet.swap(myOtherset); // Exchanges the two sets

Exchanges the contents of this set and another set.