set::erase in C++ STL

06 Oct, 2021
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. 
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 Exceptions1. 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
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
// 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
 
Dsv
hritikbhatnagar2182
cpp-set
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.