set::erase in C++ STL
Last Updated :
27 Mar, 2024
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)
3. setname.erase(value)
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, 7, 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
// INTEGER SET EXAMPLE
// CPP program to illustrate
// Implementation of erase() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// set declaration
set<int> myset{ 1, 2, 3, 4, 5 };
set<int>::iterator it1, it2;
// defining it1 pointing to the first
// element and it2 to the last element
it1 = myset.begin();
it2 = myset.end();
// decrementing the it2 two times
it2--;
it2--;
// erasing elements within the range
// of it1 and it2
myset.erase(it1, it2);
// Printing the set
for (auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
4 5
CPP
// CHARACTER SET EXAMPLE
// CPP program to illustrate
// Implementation of erase() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// set declaration
set<char> myset{ 'A', 'C', 'E', 'G' };
set<char>::iterator it1, it2;
// defining it1 pointing to the first
// element and it2 to the last element
it1 = myset.begin();
it2 = myset.end();
// decrementing the it2 two times
it2--;
it2--;
// erasing elements within the
// range of it1 and it2
myset.erase(it1, it2);
// Printing the set
for (auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
E G
Removing elements within a range
CPP
// INTEGER SET EXAMPLE
// CPP program to illustrate
// Implementation of erase() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// set declaration
set<int> myset{ 1, 2, 3, 4, 5 };
set<int>::iterator it;
// defining iterator pointing
// to the first element
it = myset.begin();
// erasing the first element
myset.erase(it);
// Printing the set
for (auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
2 3 4 5
CPP
// CHARACTER SET EXAMPLE
// CPP program to illustrate
// Implementation of erase() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// set declaration
set<char> myset{ 'A', 'B', 'C', 'D' };
set<char>::iterator it;
// defining iterator pointing
// to the first element
it = myset.begin();
// erasing the first element
myset.erase(it);
// Printing the set
for (auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
B C D
Removing elements within the given value
C++
// CHARACTER SET EXAMPLE
// CPP program to illustrate
// Implementation of erase() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// set declaration
set<char> myset{ 'A', 'B', 'C', 'D' };
// erasing the element with value C
myset.erase('C');
// Printing the set
for (auto it = myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
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.
3. setname.erase(value) – O(log n)
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
// CPP program to illustrate
// Application of erase() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// set declaration
set<int> myset{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// checking for even elements and removing them
for (auto i = myset.begin(); i != myset.end(); ) {
if (*i % 2 == 0)
i=myset.erase(i);
else
i++;
}
// Printing the set
for (auto it = myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output :
1 3 5 7 9
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...