- 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 - <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++ multiset Library - erase() Function
Description
The multiset::erase() function in C++ STL is used to remove elements from the multiset container. It can be used to erase a single element, a range of elements, or all elements with a specific value from the multiset.
Syntax
Following are the syntax of multiset::erase() function in different scenarios −
// Erasing a single element erase(const_iterator position); // Erasing all occurrences of an element erase(const key_type& value); // Erasing a range of elements 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 is 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 depends on the number of elements erased. The return values are given below −
- It returns an iterator if we are erasing a single element.
- For range based and for erasing occurrences of an element, it returns the number of elements removed from the multiset.
Exceptions
If an exception is thrown, the container remains unchanged.
Time complexity
- The time complexity of multiset::erase() function is O(log n) for erasing a single element.
- The time complexity of multiset::erase() function is O(log n + k), where k represents the number of elements to be removed.
Examples of multiset::erase() Function
The following examples demonstrate the usage of multiset::erase() function in multiset −
Erasing a Single Element
Below is an example to remove a single element from the multiset numbers using the multiset::erase() function with an iterator −
#include <iostream>
#include <set>
using namespace std;
void printEle(const multiset<int> &numbers){
for (auto it = numbers.begin(); it != numbers.end(); ++it)
cout << " " << *it;
cout << endl;
}
int main(){
multiset<int> numbers = {10, 20, 30, 40, 50};
cout << "Multiset elements:";
printEle(numbers);
auto it = numbers.find(30);
if (it != numbers.end()){
numbers.erase(it);
cout << "Multiset elements after erasing 30:";
printEle(numbers);
}
return 0;
}
The output of the above code is given below −
Multiset elements: 10 20 30 40 50 Multiset elements after erasing 30: 10 20 40 50
Erasing Multiple Elements
In this example, the erase() function is used to remove a range of elements from the 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 <set>
using namespace std;
int main() {
multiset<int> numbers = {5, 10, 15, 20, 25, 30, 35, 40};
cout << "Original multiset: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
auto first = numbers.find(15);
auto last = numbers.find(30);
if (first != numbers.end() && last != numbers.end()) {
numbers.erase(first, last);
cout << "Multiset after erasing from 15 to 30: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
}
return 0;
}
The output of the above code is given below −
Original multiset: 5 10 15 20 25 30 35 40 Multiset after erasing from 15 to 30: 5 10 30 35 40
Erasing All Occurrences of an Element
Here is an example to erase all occurrences of element "85" from the multiset scores using the erase() function.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> scores = {85, 90, 85, 95, 85, 90, 100};
cout << "Original multiset: ";
for (int score : scores)
cout << score << " ";
cout << endl;
scores.erase(85);
cout << "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 multiset: 85 85 85 90 90 95 100 Multiset after erasing all occurrences of 85: 90 90 95 100 Remaining size: 4
Erasing All Elements
You can use erase() function to erase all the elements of multiset using begin() and end() function −
#include <iostream>
#include <set>
using namespace std;
int main(){
multiset<int> numbers = {1, 2, 3, 4};
cout << "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 −
Multiset size before erase: 4 Size after erase: 0
Erasing Non-Existent Element
The following example erases a non-existent element from the multiset. The erase() function will returns 0 as shown in below example and multiset remains unchanged −
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> numbers = {10, 20, 30, 40};
cout << "Original multiset: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
int count = numbers.erase(50);
cout << "Number of elements erased: " << count << endl;
cout << "Multiset after erasing 50: ";
for (int n : numbers)
cout << n << " ";
cout << endl;
return 0;
}
The output of the code is given below −
Original multiset: 10 20 30 40 Number of elements erased: 0 Multiset after attempting to erase 50: 10 20 30 40