set::erase in C++ STL

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 behaviour otherwise.

Removing element from particular position

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

4 5
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

E G

Removing elements within a range

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

2 3 4 5
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output :

1 3 5 7 9


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Dsv



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.