Prerequisite : multiset
The multiset::erase() is the STL function in C++ removes the specified element from multiset.
There are three versions of this method. These are:
-
Syntax:
void erase (iterator position_of_iterator);
Parameters: This method accepts following parameters:
- position_of_iterator: It refers to the position of the specific element to be removed with the help of iterator.
Return value: This method returns the iterator following the removed element.
Below examples illustrate the working of multiset::erase() method:
// C++ program to demonstrate// multiset::erase() method#include <bits/stdc++.h>usingnamespacestd;intmain(){// Initialise the multisetmultiset<int> multi_set;multiset<int>::iterator ms_iterator;// Add values to the multisetfor(inti = 1; i < 10; i++) {multi_set.insert(i);}cout <<"Original multiset: ";for(ms_iterator = multi_set.begin();ms_iterator != multi_set.end();++ms_iterator)cout<<' '<< *ms_iterator;cout <<'\n';ms_iterator = multi_set.begin();ms_iterator++;// Passing the iterator for the position// at which the value is to be erasedmulti_set.erase(ms_iterator);cout <<"Modified multiset: ";for(ms_iterator = multi_set.begin();ms_iterator != multi_set.end();++ms_iterator)cout <<' '<< *ms_iterator;cout <<'\n';return0;}chevron_rightfilter_noneOutput:Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 3 4 5 6 7 8 9
-
Syntax:
size_type erase (const value_type& contant_value);
Parameters: This method accepts following parameters:
- constant_value: It refers to the specific element to be removed from the multiset with the help of its value. It must be constant. All instances of this value is erased by this method.
Return value: This method returns the iterator following the removed element.
Below examples illustrate the working of multiset::erase() method:
// C++ program to demonstrate// multiset::erase() method#include <bits/stdc++.h>usingnamespacestd;intmain(){// Initialise the multisetmultiset<int> multi_set;multiset<int>::iterator ms_iterator;// Add values to the multisetfor(inti = 1; i < 10; i++) {multi_set.insert(i);}cout <<"Original multiset: ";for(ms_iterator = multi_set.begin();ms_iterator != multi_set.end();++ms_iterator)cout <<' '<< *ms_iterator;cout <<'\n';ms_iterator = multi_set.begin();// Passing constant value to be erasedmulti_set.erase(2);cout <<"Modified multiset: ";for(ms_iterator = multi_set.begin();ms_iterator != multi_set.end();++ms_iterator)cout <<' '<< *ms_iterator;cout <<'\n';return0;}chevron_rightfilter_noneOutput:Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 3 4 5 6 7 8 9
-
Syntax:
void erase (iterator starting_iterator, iterator ending_iterator);
Parameters: This method accepts following parameters:
- starting_iterator: It refers to the starting iterator of the range of values to be removed from the multiset.
- ending_iterator: It refers to the ending iterator of the range of values to be removed from the multiset.
Return value: This method returns the number of removed elements.
Below examples illustrate the working of multiset::erase() method:
// C++ program to demonstrate// multiset::erase() method#include <bits/stdc++.h>usingnamespacestd;intmain(){// Initialise the multisetmultiset<int> multi_set;multiset<int>::iterator ms_iterator;// Add values to the multisetfor(inti = 1; i < 10; i++) {multi_set.insert(i);}cout <<"Original multiset: ";for(ms_iterator = multi_set.begin();ms_iterator != multi_set.end();++ms_iterator)cout <<' '<< *ms_iterator;cout <<'\n';ms_iterator = multi_set.begin();ms_iterator++;ms_iterator++;// Passing the iterator range for the positions// at which the values are to be erasedmulti_set.erase(ms_iterator, multi_set.end());cout <<"Modified multiset: ";for(ms_iterator = multi_set.begin();ms_iterator != multi_set.end();++ms_iterator)cout <<' '<< *ms_iterator;cout <<'\n';return0;}chevron_rightfilter_noneOutput:Original multiset: 1 2 3 4 5 6 7 8 9 Modified multiset: 1 2
Recommended Posts:
- set::erase in C++ STL
- unordered_map erase in C++ STL
- map erase() function in C++ STL
- multimap::erase() in C++ STL
- std::string::erase in C++
- unordered_multimap erase in C++ STL
- list erase() function in C++ STL
- unordered_multiset erase() function in C++ STL
- vector erase() and clear() in C++
- unordered_set erase() function in C++ STL
- Different ways to delete elements in std::map (erase() and clear())
- Difference between std::remove and vector::erase for vectors
- multiset::swap() in C++ STL
- multiset::operator= in C++ STL
- multiset::emplace() 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 contribute@geeksforgeeks.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.



