C++ unordered_set - erase() Function
The C++ unordered_set::erase function is used to delete either a single element or a range of elements from the unordered_set. It reduces the size of the unordered_set by number of elements deleted from the container.
Syntax
//Version 1 - Delete element at position iterator erase (const_iterator position); //Version 2 - Delete val from the unordered_set size_type erase (const value_type& val); //Version 3 - Delete a range of elements in [first, last) iterator erase (const_iterator first, const_iterator last);
Parameters
position |
Iterator specifying position of the element in the unordered_set to be removed. |
val |
Value to be removed from the unordered_set. |
first |
Iterator specifying position of the first element of the range in the unordered_set. Elements in [first, last) position range will be deleted. |
last |
Iterator specifying position of the last element of the range in the unordered_set. Elements in [first, last) position range will be deleted. |
Return Value
For version 2: the function returns number of elements erased. For other versions, the function returns an iterator pointed to the element that follows the last element removed. If the last element is removed, then the iterator will point to the unordered_set::end.
Time Complexity
Average case: Linear in the number of elements removed for version 1 and version 2.
Worst case: Linear in the container size..
Example:
In the example below, the unordered_set::erase function is used to delete a single element from uSet.
#include <iostream> #include <unordered_set> using namespace std; int main (){ unordered_set<int> uSet{10, 20, 30, 40, 50}; unordered_set<int>::iterator it; cout<<"uSet contain: "; for(it = uSet.begin(); it != uSet.end(); ++it) cout<<*it<<" "; //version 1: deletes element by position it = uSet.begin(); it++; uSet.erase(it); //version 2: deletes 10 from the unordered_set uSet.erase(10); cout<<"\nuSet contain: "; for(it = uSet.begin(); it != uSet.end(); ++it) cout<<*it<<" "; return 0; }
The output of the above code will be:
uSet contain: 50 40 30 20 10 uSet contain: 50 30 20
Example:
A range of elements can also be deleted from the unordered_set. Consider the example below:
#include <iostream> #include <unordered_set> using namespace std; int main (){ unordered_set<int> uSet{10, 20, 30, 40, 50}; unordered_set<int>::iterator start_it; unordered_set<int>::iterator stop_it; unordered_set<int>::iterator it; cout<<"uSet contain: "; for(it = uSet.begin(); it != uSet.end(); ++it) cout<<*it<<" "; //setting the start position at //third element of the set start_it = uSet.begin(); start_it++; start_it++; //setting the stop position at end of the set stop_it = uSet.end(); //version 3: erase a range of elements uSet.erase(start_it, stop_it); cout<<"\nuSet contain: "; for(it = uSet.begin(); it != uSet.end(); ++it) cout<<*it<<" "; return 0; }
The output of the above code will be:
uSet contain: 50 40 30 20 10 uSet contain: 50 40
❮ C++ <unordered_set> Library