This tutorial will discuss about the set erase() function in C++ stl.
Table Of Contents
In C++ Standard Template Library (STL), the set is a container that stores unique elements following a specific order. One of the useful member functions of the set container is erase(), which is utilized to remove elements from the set.
Overview of set::erase() Overloaded Versions
The set::erase() function comes in three primary overloaded versions:
- erase(const value_type& value): Erases the element with the specified value.
- erase(iterator position): Erases the element pointed to by the given iterator.
- erase(iterator first, iterator last): Erases elements in the range [first, last).
We will discuss them one by one
Erase element from Set by Value
Syntax
size_type erase (const value_type& val);
This overloaded version of erase() function allows us to remove an element from the set based on its value. We can directly pass the value to the erase function, and it will remove the corresponding element. Additionally, it returns the number of elements removed. Since a set contains only unique elements, it will always return 1, if the provided element previously existed in the set.
Frequently Asked:
Parameters:
– value: The value of the element that needs to be removed.
Returns:
– The number of elements deleted.
Example:
Let’s see the complete example,
#include <iostream> #include <set> int main() { std::set<int> numbers = {13, 27, 46, 82, 51}; // Remove element 46 from set numbers.erase(46); // Print all elements of set for (int num : numbers) { std::cout << num << " "; } std::cout<< std::endl; return 0; }
Output
13 27 51 82
Erase element from Set by Iterator
This overloaded version of erase accepts an iterator pointing to an element in the set and removes that element. It also returns an iterator pointing to the element immediately after the removed one.
iterator erase (const_iterator position);
Parameters:
– position: An iterator pointing to a single element in the set which needs to be removed.
Returns:
– The Iterator pointing to element next to last deleted element.
To utilize this overloaded function to remove an element from the set, we first need the iterator that points to the desired element. In the example below, we’ll first search for an element based on its value. Once we have the iterator pointing to that value, we can then call the erase() function to remove that specific element from the set.
Let’s see the complete example,
#include <iostream> #include <set> int main() { std::set<int> numbers = {13, 27, 46, 82, 51}; // Find the element in Set std::set<int>::iterator it = numbers.find(38); // If element exist in the Set if (it != numbers.end()) { // Remove element pointed by iterator numbers.erase(it); } // Print all elements of set for (int num : numbers) { std::cout << num << " "; } std::cout << std::endl; return 0; }
Output
13 27 46 51 82
Erase Multiple elements from Set
iterator erase (const_iterator first, const_iterator last);
In this version of the set::erase() function, we can pass two iterators: one pointing to the start and the other to the end. It will remove all elements within this range from the set.
Parameters:
– first: An iterator referring to the first element in the range to be removed.
– last: An iterator referring to the element just past the last element to be removed.
Returns:
– The Iterator pointing to element next to last deleted element.
In the below example, we aim to remove elements between number 39 and 56. Fetch the iterators pointing to these numbers and pass to erase() function, it will remove all elements between them.
Let’s see the complete example,
#include <iostream> #include <algorithm> #include <set> int main() { std::set<int> numbers = {13, 27, 46, 82, 51}; auto it1 = numbers.find(39); auto it2 = numbers.find(56); if (it1 != numbers.end() && it2 != numbers.end()) { // Erasing elements // from 39 to 56 (excluding 56) numbers.erase(it1, it2); } // Print all elements of set for (int num : numbers) { std::cout << num << " "; } std::cout << std::endl; return 0; }
Output
13 27 46 51 82
It removed multiple elements from Set.
Exceptions and Undefined Behaviors with set::erase()
- Invalid Iterator: Using an invalid iterator with erase() function (e.g., an iterator to a different container or a past-the-end iterator) will lead to undefined behavior.
- Range Error: For the range version of the erase() function, if last comes before first (i.e., the range is invalid), it results in undefined behavior.
- Iterator Invalidation: After the erase() function is used, all iterators, pointers, and references to the erased elements are invalidated. Using them will lead to undefined behavior.
The set::erase() function gives a robust way to remove elements from set. It’s essential to be aware of potential pitfalls, like iterator invalidation and the importance of providing valid iterators, to avoid undefined behavior. Always ensure your iterators and ranges are correct when using the set::erase() function.
Pointers in C/C++ [Full Course]
Summary
Today, we learned about set erase() function in C++ stl.