Remove element from set c++ – Different ways to Erase / Delete an element from a Set in C++ | set::erase() Function in C++

Remove element from set c++: In this article, we will be going to discuss various ways to erase an element from a Set in C++.

erase() function is used to remove elements from a set from the specified range.

Here, std::set gives 3 overloaded version of erase() member function. So, we will learn them one by one clearly in this tutorial.

First, let’s take a string which we will use as an example,

#include <iostream>
#include <set>
#include <iterator>
#include <string>
#include <vector>

int main() {
   //Set Of Strings 
   std::set<std::string> setOfStrs = {"Good", "Morning", "say", "hello", "at", "Hi", "is", "from", "to"};
    // Printing Contents of Set
    std::copy (setOfStrs.begin(), setOfStrs.end(), std::ostream_iterator<std::string>(std::cout, ", "));
    std::cout<<std::endl;
    return 0;
}

Output:

Good, Hi, Morning, at, from, hello, is, say, to

Now we will remove elements from it in three different ways:

Let’s start with the first way of erase/ delete an element from a set in C++

Removing Element from set By Iterator

Set erase c++: An element that has to be erased will be given in the iterator. Iterator is accepted by the overloaded version of erase() function which provided by ‘std::set’.

Syntax:

iterator erase (const_iterator position);

It renders the element close to the last deleted element. Let’s apply this to remove an element from the above set of strings i.e.,

#include <iostream>
#include <set>
#include <iterator>
#include <string>
#include <vector>

int main() {
  
   //Set Of Strings 
   std::set<std::string> setOfStrs = {"Good", "Morning", "say", "hello", "at", "Hi", "is", "from", "to"};
    
    // Search for element "is"
    std::set<std::string>::iterator it = setOfStrs.find("is");
    // Check if Iterator is valid
    if(it != setOfStrs.end())
    {
        // Deletes the element pointing by iterator it
        setOfStrs.erase(it);
    }
    // Printing Contents of Set
    std::copy (setOfStrs.begin(), setOfStrs.end(), std::ostream_iterator<std::string>(std::cout, ", "));
    std::cout<<std::endl;
    return 0;
}

Output:

Good, Hi, Morning, at, from, hello, say, to

So you have seen this is how we erase elements from the given string. You can see that above we have erased “is” from our given string.

Read More:

Removing Element from set By Value

“std::set” presents an overloaded version of an erase() function that allows a value and delete that from the set.

Syntax:

size_type erase (const value_type& val);

Let’s view the following example code on removing an element from the above set of strings i.e.,

#include <iostream>
#include <set>
#include <iterator> 
#include <string>
#include <vector>
int main() {
    //Set Of Strings 
    std::set<std::string> setOfStrs = {"Good", "Morning", "say", "hello", "at", "Hi", "is", "from", "to"};
    // Erase element "that" from set
    setOfStrs.erase("that");
    // Printing Contents of Set
    std::copy (setOfStrs.begin(), setOfStrs.end(), std::ostream_iterator<std::string>(std::cout, ", "));
    std::cout<<std::endl;
    return 0;
}

Output:

Good, Hi, Morning, at, from, hello, is, say, to

So above you can see that we remove ‘that’ from our string.

Removing Element from set By Iterator Range

In this approach, ‘std::set’ gives an overloaded version of erase() function that takes two iterators representing a range from (start) to (end -1).

Syntax:

iterator erase (const_iterator first, const_iterator last);

Now, we will use this to remove elements from the above set of strings i.e.,

#include <iostream>
#include <set>
#include <iterator> 
#include <string>
#include <vector>
int main() {
    //Set Of Strings 
    std::set<std::string> setOfStrs = {"Good", "Morning", "say", "hello", "at", "Hi", "is", "from", "to"};
    // Iterator pointing to "Hello" in Set
std::set<std::string>::iterator start = setOfStrs.find("Hello");
// Iterator pointing to "from" in Set
std::set<std::string>::iterator last = setOfStrs.find("from");
// Check if both start and last iterators are valid
if(start != setOfStrs.end() && last != setOfStrs.end())
{
    // Erase all elements from "Hello" to "from"
    setOfStrs.erase(start, last);
}

    return 0;
}

Output:

Good, Morning, say, to

So you can see that it has remove all the elements in the given range and returns the element next to the last deleted element.

Conclusion:

In this article, you have seen different ways to erase/delete an element from a Set in C++. Keep browsing our C++, CSS, HTML, Python articles on BtechGeeks and improve your coding skills more and more. Thank you!