Skip to content
Related Articles

Related Articles

unordered_set erase() function in C++ STL
  • Difficulty Level : Easy
  • Last Updated : 28 Sep, 2018

The unordered_set::erase() function is a built-in function in C++ STL which is used to remove either a single element of a range of elements ranging from start(inclusive) to end(exclusive). This decreases the size of a container by the number of elements removed.

Note: Buckets in unordered_set are numbered from 0 to n-1, where n is the total number of buckets.

Syntax:

unordered_set_name.erase(iterator start, iterator end)
          or 
unordered_set_name.erase(iterator position)
          or
unordered_set_name.erase(element)

Parameters: The function accepts three type of parameters. If it accepts a single element, then it finds that particular element and erases it. If it accepts an iterator, then it erases the element present at that position. If it accepts two iterators start and end, it erases all the elements in the range [start, end]

Return Value: This function returns an iterator pointing to the element following the last element which is erased in case of first two syntaxes. In case of the third syntax, it returns 1 if the element is present in the unordered_set else it returns 0 after erasing the element



Below programs illustrate the unordered_set::erase() function:

Program 1:




// CPP program to illustrate the
// unordered_set::erase() function
  
#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<int> sampleSet;
  
    // Inserting elements
    sampleSet.insert(5);
    sampleSet.insert(10);
    sampleSet.insert(15);
    sampleSet.insert(20);
    sampleSet.insert(25);
  
    // erases a particular element by its position
    sampleSet.erase(sampleSet.find(10));
  
    // displaying the set after removal
    for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) {
        cout << *it << " ";
    }
  
    // erases a range of elements
    sampleSet.erase(sampleSet.begin(), sampleSet.end());
  
    cout << "\nSet size: " << sampleSet.size();
  
    return 0;
}

Output:

25 5 15 20  
Set size: 0

Program 2:




// CPP program to illustrate the
// unordered_set::erase() function
  
#include <iostream>
#include <string>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" };
  
    // erases a particular element
    sampleSet.erase("geeks1");
  
    // displaying the set after removal
    cout << "Elements: ";
    for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) {
        cout << *it << " ";
    }
  
    sampleSet.insert("geeks1");
    // erases from where for is
    sampleSet.erase(sampleSet.find("for"), sampleSet.end());
  
    // displaying the set after removal
    cout << "\nAfter second removal set : ";
    for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) {
        cout << *it << " ";
    }
  
    return 0;
}

Output:

Elements: geeks2 for 
After second removal set : geeks1 geeks2 
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :