multiset erase() in C++ STL
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>
using
namespace
std;
int
main()
{
// Initialise the multiset
multiset<
int
> multi_set;
multiset<
int
>::iterator ms_iterator;
// Add values to the multiset
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++;
// Passing the iterator for the position
// at which the value is to be erased
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:
// C++ program to demonstrate
// multiset::erase() method
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// Initialise the multiset
multiset<
int
> multi_set;
multiset<
int
>::iterator ms_iterator;
// Add values to the multiset
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();
// Passing constant value to be erased
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:
// C++ program to demonstrate
// multiset::erase() method
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// Initialise the multiset
multiset<
int
> multi_set;
multiset<
int
>::iterator ms_iterator;
// Add values to the multiset
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++;
// Passing the iterator range for the positions
// at which the values are to be erased
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();
Contact Us