- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <unordered_multiset>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ unordered_multiset::erase() Function
Description
The unordered_multiset::erase() function in C++ STL is used to remove elements from the unordered_multiset container. It can be used to erase a single element, a range of elements, or all elements with a specific value from the unordered_multiset.
Syntax
Following are the syntax of unordered_multiset::erase() function in different scenarios −
// Erasing a single element iterator erase(const_iterator position); // Erasing all occurrences of an element size_type erase(const key_type& value); // Erasing a range of elements iterator erase(const_iterator first, const_iterator last);
Parameters
The parameters of erase() function are as follows −
- position − It represents an iterator pointing to the element that we want to remove.
- value − It represents the value of the element whose all occurrences are to be removed.
- first, last − It represents starting and ending iterators of range of elements to be removed.
Return value
The return value of erase() function are given below −
- It returns an iterator if we are erasing a single element.
- For range-based, it returns an iterator that points to the element just after the last removed element.
- For erasing all occurrences of a value, it returns the number of elements (size_type) removed from the unordered_multiset.
Exceptions
If an exception is thrown, the container remains unchanged.
Time complexity
- The time complexity of unordered_multiset::erase() function is O(1) on average for erasing a single element.
- The time complexity of unordered_multiset::erase() function is O(n) in worst case, where n represents the number of elements in the container.
Examples of unordered_multiset::erase() Function
The following examples demonstrate the usage of unordered_multiset::erase() function in unordered_multiset −
Erasing a Single Element
Below is an example to remove a single element from the unordered_multiset numbers using the unordered_multiset::erase() function with an iterator −
#include <iostream>
#include <unordered_set>
using namespace std;
void printEle(const unordered_multiset<int> &numbers) {
for (auto it = numbers.begin(); it != numbers.end(); ++it)
cout << " " << *it;
cout << endl;
}
int main() {
unordered_multiset<int> numbers = {10, 20, 30, 40, 50};
cout << "Unordered_multiset elements:";
printEle(numbers);
auto it = numbers.find(30);
if (it != numbers.end()){
numbers.erase(it);
cout << "Unordered_multiset elements after erasing 30:";
printEle(numbers);
}
return 0;
}
The output of the above code is given below −
Unordered_multiset elements: 50 40 30 20 10 Unordered_multiset elements after erasing 30: 50 40 20 10
Erasing Multiple Elements
In this example, the erase() function is used to remove a range of elements from the unordered_multiset. The first iterator points to the starting point of the range and the last iterator points to the position till we want to erase. The starting element is inclusive and the ending element is exclusive.
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> numbers = {5, 10, 15, 20, 25, 30};
cout << "Original unordered_multiset: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
// Erasing first 3 elements
auto it_first = numbers.begin();
auto it_last = numbers.begin();
for (int i = 0; i < 3 && it_last != numbers.end(); i++)
++it_last;
numbers.erase(it_first, it_last);
cout << "After erasing first 3 elements: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
return 0;
}
The output of the above code is given below −
Original unordered_multiset: 30 25 20 15 10 5 After erasing first 3 elements: 15 10 5
Erasing All Occurrences of an Element
Here is an example to erase all occurrences of element "85" from the unordered_multiset scores using the erase() function.
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> scores = {85, 90, 85, 95, 85, 90, 100};
cout << "Original unordered_multiset: ";
for (int score : scores)
cout << score << " ";
cout << endl;
int count = scores.erase(85);
cout << "Number of elements erased: " << count << endl;
cout << "Unordered_multiset after erasing all occurrences of 85: ";
for (int score : scores)
cout << score << " ";
cout << endl;
cout << "Remaining size: " << scores.size() << endl;
return 0;
}
The output of the above code is given below −
Original unordered_multiset: 100 90 90 95 85 85 85 Number of elements erased: 3 Unordered_multiset after erasing all occurrences of 85: 100 90 90 95 Remaining size: 4
Erasing All Elements
You can use erase() function to erase all the elements of unordered_multiset using begin() and end() function −
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> numbers = {1, 2, 3, 4};
cout << "Unordered_multiset size before erase: "
<< numbers.size() << endl;
numbers.erase(numbers.begin(), numbers.end());
cout << "Size after erase: "
<< numbers.size() << endl;
return 0;
}
The output of the code is given below −
Unordered_multiset size before erase: 4 Size after erase: 0
Erasing Non-Existent Element
The following example erases a non-existent element from the unordered_multiset. The erase() function will returns 0 as shown in below example and unordered_multiset remains unchanged −
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> numbers = {10, 20, 30, 40};
cout << "Original unordered_multiset: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
int count = numbers.erase(50);
cout << "Number of elements erased: " << count << endl;
cout << "Unordered_multiset after attempting to erase 50: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
return 0;
}
The output of the code is given below −
Original unordered_multiset: 40 30 20 10 Number of elements erased: 0 Unordered_multiset after attempting to erase 50: 40 30 20 10
Difference Between multiset::erase and unordered_multiset::erase
The differences between multiset::erase and unordered_multiset::erase functions are given below −
| multiset::erase | unordered_multiset::erase |
|---|---|
| The multiset::erase() function works on a multiset. | The unordered_multiset::erase() function works on an unordered multiset. |
| It uses a tree-based structure for locating and removing elements using comparisons. | It calculates hash value and then scans its corresponding hash bucket for locating and removing elements. |
| The time complexity is O(log n) for erasing a single element. | The time complexity is O(1) on average for erasing a single element and O(n) in the worst case. |
| After erasing elements, the remaining elements stay in sorted order. | After erasing elements, there is no fixed order for the remaining elements. |