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:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > multi_set;
multiset< int >::iterator ms_iterator;
for ( int i = 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++;
multi_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' ;
return 0;
}
|
Output:
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 no. of values that is/are removed.
Below examples illustrate the working of multiset::erase() method:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > multi_set;
multiset< int >::iterator ms_iterator;
for ( int i = 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();
int num = multi_set.erase(2);
cout << "Modified multiset: "
<< "(" << num << ")"
<< "removed" ;
for (ms_iterator = multi_set.begin();
ms_iterator != multi_set.end();
++ms_iterator)
cout << ' ' << *ms_iterator;
cout << '\n' ;
return 0;
}
|
Output:
Original multiset: 1 2 3 4 5 6 7 8 9
Modified multiset:(1)removed 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 iterator following the last removed element or end iterator.
Below examples illustrate the working of multiset::erase() method:
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > multi_set;
multiset< int >::iterator ms_iterator;
for ( int i = 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++;
auto ir = multi_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' ;
(ir == multi_set.end())
? cout << "Return value is: multi_set.end()\n "
: cout
<< "Return value is not multi_set.end()\n" ;
return 0;
}
|
Output:
Original multiset: 1 2 3 4 5 6 7 8 9
Modified multiset: 1 2
Return value is: multi_set.end();
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Jan, 2020
Like Article
Save Article