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.

Suppose we have a set of strings i.e.

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.

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.

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.

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.

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.

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.

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.