We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Different ways to Erase / Delete an element from a Set in C++

In this article we will discuss different ways to remove an element from Set.


std::set provides 3 overloaded version of erase() member function. We will discuss them one by one.

Advertisements

Suppose we have a set of strings i.e.

//Set Of Strings
std::set<std::string> setOfStrs = {"Hi", "Hello", "is", "the", "at", "Hi", "is", "from", "that"};

Let’s remove elements from it i.e.

Removing element from set By Iterator

std::set provides an overloaded version of erase function that accepts an iterator and removes the element pointed by the iterator i.e.

iterator  erase (const_iterator position);

It returns the element next to the last deleted element. Let’s use this to remove an element from above set of strings i.e.

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

Removing element from set By Value

std::set provides an overloaded version of erase function that accepts a value and removes that from set i.e.

size_type erase (const value_type& val);

It returns the number of elements deleted. As std::set contains only unique elements, so its value will always be 1.

Let’s use this to remove an element from above set of strings i.e.

// Erase element "that" from set
setOfStrs.erase("that");

Removing element from set By Iterator Range

std::set provides an overloaded version of erase function that accepts two iterators representing a range from (start) to (end -1) i.e.

iterator  erase (const_iterator first, const_iterator last);

It removes all the elements in the given range and returns the element next to the last deleted element.

Let’s use this to remove elements from above set of strings i.e.

// Iterator pointing to "Hi" in Set
std::set<std::string>::iterator start = setOfStrs.find("Hi");
// 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 "Hi" to "from"
	setOfStrs.erase(start, last);
}

Complete example is as follows,

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

int main()
{
	//Set Of Strings
	std::set<std::string> setOfStrs = {"Hi", "Hello", "is", "the", "at", "Hi", "is", "from", "that"};

	// Printing Contents of Set
	std::copy (setOfStrs.begin(), setOfStrs.end(), std::ostream_iterator<std::string>(std::cout, ", "));
	std::cout<<std::endl;

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

	// Iterator pointing to "Hi" in Set
	std::set<std::string>::iterator start = setOfStrs.find("Hi");
	// 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 "Hi" to "from"
		setOfStrs.erase(start, last);
	}

	// Printing Contents of Set
	std::copy (setOfStrs.begin(), setOfStrs.end(), std::ostream_iterator<std::string>(std::cout, ", "));
	std::cout<<std::endl;

	// 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:

Hello, Hi, at, from, is, that, the, 
Hello, Hi, at, from, that, the, 
Hello, from, that, the, 
Hello, from, the,

 

Cart

Your Cart is Empty

Back To Shop