Sets are a type of ordered associative containers in which each element has to be unique because the value of the element identifies it. The values are stored in a specific order.
Can be used to implement Binary Search Tree.
set setname; // ascending order
set<datatype, greater> setname; // descending order
- The set stores the elements in sorted order.
- All the elements in a set have unique values.
- The value of the element cannot be modified once it is added to the set, though it is possible to remove and then add the modified value of that element. Thus, the values are immutable.
- Sets follow the Binary search tree implementation.
- The values in a set are unindexed.
- begin() – Returns an iterator to the first element in the set.
- end() – Returns an iterator to the theoretical element that follows the last element in the set.
- size() – Returns the number of elements in the set.
- empty() – Returns whether the set is empty.
- insert(const g) - Adds a new element ‘g’ to the set.
- erase(iterator position) - Removes the element at the position pointed by the iterator.
- erase(const g) - Removes the value ‘g’ from the set.
- clear() - Removes all the elements from the set.
- find(const g) - Returns an iterator to the element ‘g’ in the set if found, else returns the iterator to end.
- lower_bound(const g) - Returns an iterator to the first element that is equivalent to ‘g’ or definitely will not go before the element ‘g’ in the set.
- upper_bound(const g) - Returns an iterator to the first element that will go after the element ‘g’ in the set.