Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.
set::erase()
erase() function is used to remove elements from a container from the specified position or range.
Syntax :
1. setname.erase(position)
2. setname.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in
the form of iterator or the range specified
using start and end iterator.
Result :
Elements are removed from the specified
position of the container.
Examples:
Input : myset{1, 2, 3, 4, 5}, iterator= 2
myset.erase(iterator);
Output : 1, 2, 4, 5
Input : myset{1, 2, 3, 4, 5, 6, 7, 8},
iterator1= 3, iterator2= 6
myset.erase(iterator1, iterator2);
Output : 1, 2, 3, 8
Errors and Exceptions
1. It has a no exception throw guarantee, if the position is valid.
2. Shows undefined behavior otherwise.
Removing element from particular position
CPP
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int > myset{ 1, 2, 3, 4, 5 };
set< int >::iterator it1, it2;
it1 = myset.begin();
it2 = myset.end();
it2--;
it2--;
myset.erase(it1, it2);
for ( auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
4 5
CPP
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< char > myset{ 'A' , 'C' , 'E' , 'G' };
set< char >::iterator it1, it2;
it1 = myset.begin();
it2 = myset.end();
it2--;
it2--;
myset.erase(it1, it2);
for ( auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
E G
Removing elements within a range
CPP
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int > myset{ 1, 2, 3, 4, 5 };
set< int >::iterator it;
it = myset.begin();
myset.erase(it);
for ( auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
2 3 4 5
CPP
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< char > myset{ 'A' , 'B' , 'C' , 'D' };
set< char >::iterator it;
it = myset.begin();
myset.erase(it);
for ( auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
B C D
Time Complexity:
1. setname.erase(position) – amortized constant
2. setname.erase(startingposition, endingposition) – O(n), n is number of elements between starting position and ending position.
Application
Given a set of integers, remove all the even elements from the set and print the set.
Input :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :1 3 5 7 9
Explanation - 2, 4, 6 and 8 which are even are erased from the set
Algorithm
1. Run a loop till the size of the set.
2. Check if the element at each position is divisible by 2, if yes- remove the element and assign the return iterator to the current iterator, if no- increment the iterator.
3. Print the final set.
Note:erase return the iterator of the next element
CPP
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int > myset{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for ( auto i = myset.begin(); i != myset.end(); ) {
if (*i % 2 == 0)
i=myset.erase(i);
else
i++;
}
for ( auto it = myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output :
1 3 5 7 9
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 Oct, 2021
Like Article
Save Article