C++ multiset erase
The erase method in C++ Multimultiset is employed to eliminate either a solitary element linked with a specified key or a series of elements ([first, last)) from the multiset container. Consequently, the size of the container will decrease by the quantity of elements that have been deleted.
Syntax
void erase (iterator position); //until C++ 11
size_type erase (const value_type& val); //until C++ 11
void erase (iterator first, iterator last); //until C++ 11
iterator erase (const_iterator position); //since C++ 11
size_type erase (const value_type& val); //since C++ 11
iterator erase (const_iterator first, const_iterator last); //since C++ 11
Parameter
position: Iterator indicating the specific element to be eliminated from the container.
val : Value to be removed from the multiset.
first : Beginning of the range to erase.
last : End of the range to erase.
Return value
The erase method provides an iterator to the next element following the deleted element, or it returns the count of deleted elements.
Complexity
erase(position) : Amortized constant.
erase (val) : Logarithmic in container size.
Removing elements using the erase(first, last) function operates in a manner that is directly proportional to the length between the first and last elements within the container.
Iterator validity
Iterators, references, and pointers pointing to elements that have been removed by the function become invalidated. However, all other iterators, pointers, and references retain their validity.
Data Races
The container is modified.
The removed components have been altered. It is considered unsafe to iterate through ranges within the container, but accessing other elements concurrently is secure.
Exception Safety
This function does not throw exception.
Specifying an invalid range or position can lead to unpredictable behavior.
Example 1
Let's explore a straightforward example of removing an element using an iterator.
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset;
multiset<int>::iterator it;
mymultiset = {10,20,30,20};
cout<<"Before erasing the element: \n";
for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it << '\n';
it=mymultiset.find('b');
mymultiset.erase (*it); // erasing by iterator
cout<<"\nAfter erasing the element: \n";
for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it << '\n';
return 0;
}
Output:
Before erasing the element:
10
20
20
30
After erasing the element:
10
20
20
30
In the aforementioned example, the element is removed by the iterator 'it'.
Example 2
Let's examine a straightforward example demonstrating the removal of an element from a multiset using a specified key value:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset;
multiset<int>::iterator it;
mymultiset = {10, 20, 30, 40, 10, 30};
cout<<"Before erasing the element: \n";
for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it<< '\n';
mymultiset.erase (30); // erasing by value
cout<<"\nAfter erasing the element: \n";
for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it<< '\n';
return 0;
}
Output:
Before erasing the element:
10
10
20
30
30
40
After erasing the element:
10
10
20
40
In the previous example, the erase(value) method removes the value 30 from the multiset.
Example 3
Let's consider a basic example demonstrating the removal of an element within a specified range:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset;
multiset<int>::iterator it;
mymultiset = {10, 20, 30, 20};
cout<<"Before erasing the element are: \n";
cout<<"Size is: "<<mymultiset.size()<<'\n';
for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it << '\n';
mymultiset.erase ( mymultiset.begin () , mymultiset.end () ); // erasing by range
cout<<"\nAfter erasing the element are: \n";
cout<<"Size is: "<<mymultiset.size();
for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it << '\n';
return 0;
}
Output:
Before erasing the element are:
Size is: 3
10
20
30
After erasing the element are:
Size is: 0
The erase(first, last) function is employed to remove the element within the specified range, from the beginning to the end.
Example 4
Let's consider a basic illustration demonstrating the removal of all odd numbers from the multiset:
#include <set>
#include <iostream>
using namespace std;
int main()
{
multiset<int> m = {1,2,3,4,5,6,7,8,9,10,11,12,11,10,4};
// erase all odd numbers from m
cout<<"After erasing odd numbers,elements are:\n ";
for(auto it = m.begin(); it != m.end(); )
if(*it % 2 == 1)
it = m.erase(it);
else
++it;
for(auto& p : m)
cout << p << ", ";
}
Output:
After erasing odd numbers, elements are:
2, 4, 4, 6, 8, 10, 10, 12,
In the example provided, odd integers have been deleted, and only even numbers are being shown.
Example 5
Let's see another example:
#include <set>
#include <string>
#include <iostream>
#include <iterator> // next() and prev() helper functions
using namespace std;
using mymultiset = multiset<string>;
void printmultiset(const mymultiset& s) {
for (const auto& iter : s) {
cout << " [" << iter << "]";
}
cout << endl << "size() == " << s.size() << endl << endl;
}
int main()
{
mymultiset s1;
// Fill in some data to test with, one at a time
s1.insert("Bob");
s1.insert("Robert");
s1.insert("Rob");
s1.insert("Rob");
s1.insert("Bob");
cout << "Starting data of multiset s1 is:" << endl;
printmultiset(s1);
// The 1st member function removes an element at a given position
s1.erase(next(s1.begin()));
cout << "After the 2nd element is deleted, the multiset s1 is:" << endl;
printmultiset(s1);
// Fill in some data to test with, one at a time, using an intializer list
mymultiset s2{ "meow", "nikita", "nikita", "growl", "yellow" };
cout << "Starting data of multiset s2 is:" << endl;
printmultiset(s2);
// The 2nd member function removes elements
// in the range [First, Last)
s2.erase(next(s2.begin()), prev(s2.end()));
cout << "After the middle elements are deleted, the multiset s2 is:" << endl;
printmultiset(s2);
mymultiset s3;
// Fill in some data to test with, one at a time, using emplace
s3.emplace("C");
s3.emplace("C#");
s3.emplace("D");
s3.emplace("D#");
s3.emplace("E");
s3.emplace("E#");
s3.emplace("F");
s3.emplace("F#");
s3.emplace("G");
s3.emplace("G#");
s3.emplace("E");
s3.emplace("E#");
s3.emplace("B");
cout << "Starting data of multiset s3 is:" << endl;
printmultiset(s3);
// The 3rd member function removes elements with a given Key
mymultiset::size_type count = s3.erase("E#");
// The 3rd member function also returns the number of elements removed
cout << "The number of elements removed from s3 is: " << count << "." << endl;
cout << "After the element with a key of \"E#\" is deleted, the multiset s3 is:" << endl;
printmultiset(s3);
}
Output:
Starting data of multiset s1 is:
[Bob] [Bob] [Rob] [Rob] [Robert]
size() == 5
After the 2nd element is deleted, the multiset s1 is:
[Bob] [Rob] [Rob] [Robert]
size() == 4
Starting data of multiset s2 is:
[growl] [meow] [nikita] [nikita] [yellow]
size() == 5
After the middle elements are deleted, the multiset s2 is:
[growl] [yellow]
size() == 2
Starting data of multiset s3 is:
[B] [C] [C#] [D] [D#] [E] [E] [E#] [E#] [F] [F#] [G] [G#]
size() == 13
The number of elements removed from s3 is: 2.
After the element with a key of "E#" is deleted, the multiset s3 is:
[B] [C] [C#] [D] [D#] [E] [E] [F] [F#] [G] [G#]
size() == 11