The unordered_set::erase() function is a built-in function in C++ STL which is used to remove either a single element of a range 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.
unordered_set_name.erase(iterator start, iterator end) or unordered_set_name.erase(iterator position) or unordered_set_name.erase(element)
Parameters: The function accepts three type of parameters. If it accepts a single element, then it finds that particular element and erases 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 1 if the element is present in the unordered_set else it returns 0 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
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- How to erase an element from a vector using erase() and reverse_iterator?
- list erase() function in C++ STL
- unordered_multiset erase() function in C++ STL
- map erase() function in C++ STL
- deque::clear() and deque::erase() in C++ STL
- set::erase in C++ STL
- unordered_map erase in C++ STL
- multiset erase() in C++ STL
- unordered_multimap erase in C++ STL
- multimap::erase() in C++ STL
- Different ways to delete elements in std::map (erase() and clear())
- Difference between std::remove and vector::erase for vectors
- std::string::erase in C++
- vector erase() and clear() in C++
- atanh() function in C++ STL
- vector rbegin() and rend() function in C++ STL
- unordered_multimap begin() and end() function in C++ STL
- list cbegin() and cend() function in C++ STL
- deque assign() function in C++ STL
- bitset all() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.