C++ unordered_multiset - erase() Function
The C++ unordered_multiset::erase function is used to delete either a single element or a range of elements from the unordered_multiset. It reduces the size of the unordered_multiset by number of elements deleted from the container.
Syntax
//Version 1 - Delete element at position iterator erase (const_iterator position); //Version 2 - Delete all occurrences of val 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_multiset to be removed. |
val |
Value to be removed from the unordered_multiset. |
first |
Iterator specifying position of the first element of the range in the unordered_multiset. Elements in [first, last) position range will be deleted. |
last |
Iterator specifying position of the last element of the range in the unordered_multiset. 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_multiset::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_multiset::erase function is used to delete elements from uMSet.
#include <iostream> #include <unordered_set> using namespace std; int main (){ unordered_multiset<int> uMSet{10, 20, 30, 30, 30, 40, 40, 50}; unordered_multiset<int>::iterator it; cout<<"uMSet contain: "; for(it = uMSet.begin(); it != uMSet.end(); ++it) cout<<*it<<" "; //version 1: deletes element at position = 2 it = uMSet.begin(); it++; uMSet.erase(it); //version 2: deletes 30 from the multiset uMSet.erase(30); cout<<"\nuMSet contain: "; for(it = uMSet.begin(); it != uMSet.end(); ++it) cout<<*it<<" "; return 0; }
The output of the above code will be:
uMSet contain: 50 40 40 30 30 30 20 10 uMSet contain: 50 40 20 10
Example:
A range of elements can also be deleted from the unordered_multiset. Consider the example below:
#include <iostream> #include <unordered_set> using namespace std; int main (){ unordered_multiset<int> uMSet{10, 20, 20, 30, 40, 40, 50}; unordered_multiset<int>::iterator start_it; unordered_multiset<int>::iterator stop_it; unordered_multiset<int>::iterator it; cout<<"uMSet contain: "; for(it = uMSet.begin(); it != uMSet.end(); ++it) cout<<*it<<" "; //setting the start position at //third element of the multiset start_it = uMSet.begin(); start_it++; start_it++; //setting the stop position at end of the multiset stop_it = uMSet.end(); //version 3: erase a range of elements uMSet.erase(start_it, stop_it); cout<<"\nuMSet contain: "; for(it = uMSet.begin(); it != uMSet.end(); ++it) cout<<*it<<" "; return 0; }
The output of the above code will be:
uMSet contain: 50 40 40 30 20 20 10 uMSet contain: 50 40
❮ C++ <unordered_set> Library