C++ Set Library Methods
The <set> library in C++ provides a collection of methods for managing ordered sets of unique
elements.
Below is a list of the common methods available, along with their arguments and usage examples.
1) Constructor
- set<T>()
- set<T>(initializer_list<T>)
- set<T>(const set<T>&)
- set<T>(set<T>&&)
- Description: Constructs a new set.
2) insert()
- Arguments: const T& value, T&& value
- Usage: Inserts an element into the set if it's not already present.
- Example: set.insert(10);
3) erase()
- Arguments: iterator pos, const T& value, iterator first, iterator last
- Usage: Removes an element or a range of elements from the set.
- Example: set.erase(10);
4) find()
- Arguments: const T& value
- Usage: Searches the set for an element with a specific value.
- Example: auto it = set.find(10);
5) count()
- Arguments: const T& value
- Usage: Counts the occurrences of an element in the set (0 or 1).
- Example: size_t count = set.count(10);
6) size()
- Arguments: None
- Usage: Returns the number of elements in the set.
- Example: size_t size = set.size();
7) empty()
- Arguments: None
- Usage: Checks if the set is empty.
- Example: bool isEmpty = set.empty();
8) clear()
- Arguments: None
- Usage: Removes all elements from the set.
- Example: set.clear();
9) begin()
- Arguments: None
- Usage: Returns an iterator to the first element in the set.
- Example: auto it = set.begin();
10) end()
- Arguments: None
- Usage: Returns an iterator to the past-the-end element in the set.
- Example: auto it = set.end();
11) rbegin()
- Arguments: None
- Usage: Returns a reverse iterator to the first element of the reversed set.
- Example: auto rit = set.rbegin();
12) rend()
- Arguments: None
- Usage: Returns a reverse iterator to the past-the-end element of the reversed set.
- Example: auto rit = set.rend();
13) max_size()
- Arguments: None
- Usage: Returns the maximum number of elements the set can hold.
- Example: size_t maxSize = set.max_size();
14) swap()
- Arguments: set<T>& other
- Usage: Swaps the contents of two sets.
- Example: set1.swap(set2);
15) emplace()
- Arguments: Args&&... args
- Usage: Constructs and inserts an element in-place into the set.
- Example: set.emplace(10);
16) emplace_hint()
- Arguments: const_iterator position, Args&&... args
- Usage: Constructs and inserts an element in-place at a specific position.
- Example: set.emplace_hint(set.begin(), 10);
17) equal_range()
- Arguments: const T& value
- Usage: Returns a range of elements that are equivalent to a specific value.
- Example: auto range = set.equal_range(10);
18) lower_bound()
- Arguments: const T& value
- Usage: Returns an iterator pointing to the first element not less than the given value.
- Example: auto it = set.lower_bound(10);
19) upper_bound()
- Arguments: const T& value
- Usage: Returns an iterator pointing to the first element greater than the given value.
- Example: auto it = set.upper_bound(10);