unordered_set erase() function in C++ STL
The unordered_set::erase() function is a built-in function in C++ STL which is used to remove either a single element or a group of elements ranging from start(inclusive) to end(exclusive). This decreases the size of a container by the number of elements removed.
Note: Buckets in unordered_set are numbered from 0 to n-1, where n is the total number of buckets.
It can be declared in three ways,
Method(1): unordered_set_name.erase(iterator start, iterator end) Method(2): unordered_set_name.erase(iterator position) Method(3): unordered_set_name.erase(element)
Average case: Linear in the number of elements removed (which is constant for Method(2) and Method(3)).
Worst case: Linear in the container size.
Parameters: The function accepts three type of parameters. If it accepts a single element, then it finds that particular element and erases all it. If it accepts an iterator, then it erases the element present at that position. If it accepts two iterators start and end, it erases all the elements in the range [start, end]
Return Value: This function returns an iterator pointing to the element following the last element which is erased in case of first two syntaxes. In case of the third syntax, it returns 0 if the element is not present in the unordered_set else it returns 1 after erasing the element.
Below programs illustrate the unordered_set::erase() function:
25 5 15 20 Set size: 0
Elements: geeks2 for After second removal set : geeks1 geeks2
Please Login to comment...