
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete Element from Set by Value in C++
In this article, we will explain how to delete an element from a set in C++ by passing its value. A set stores elements in sorted order, where each element is unique and cannot be modified once added. While the values cannot be changed, we can add or remove elements using the erase() function from the C++ Standard Library (STL).
For example, if we have a set of integers:
{1, 2, 3, 4, 5}
and want to remove the element 3, we can use the erase() function. After the operation, the set will be
{1, 2, 4, 5}, with 3 removed.
In this article, we will show you how to implement this in C++ using the STL erase() function.
Approaches for Deleting an Element from a Set
We can delete an element from a set in C++ using two main methods with the erase() function. Here, we'll look at these methods.
Using the erase() Function with a Value
The simplest way to delete an element from a set is to use the erase() function by passing the value of the element you want to remove. The erase() function will search for the element and remove it if it exists.
Example
In this example, we will create a set, insert some elements into it, and then remove an element by passing its value. Below is the complete C++ code.
#include <iostream> #include <set> int main() { // Create a set and insert values into it std::set<int> mySet = {10, 20, 30, 40, 50}; // Display the set before deletion std::cout << "Set before deletion: "; for (int num : mySet) { std::cout << num << " "; } std::cout << std::endl; // Element to be deleted int elementToDelete = 30; // Delete the element from the set mySet.erase(elementToDelete); // Display the set after deletion std::cout << "Set after deletion of " << elementToDelete << ": "; for (int num : mySet) { std::cout << num << " "; } return 0; }
The output shows the set before and after deleting the element 30, with the remaining elements displayed in sorted order.
Set before deletion: 10 20 30 40 50 Set after deletion of 30: 10 20 40 50
Time Complexity: O(n log n) because inserting elements into the set takes O(log n) per insertion, and there are n elements.
Space Complexity: O(n) because the set stores n elements.
Using the erase() Function with an Iterator
Another way to delete an element from a set is by using an iterator. We first find the element with an iterator and then pass that iterator to the erase() function. This method is helpful when you want to delete an element based on its position in the set.
Example
In this example, we first create a set and insert some elements into it. Then we use the find() function to search for the element 40 and delete it from the set. After deleting the element, we print the updated set with the remaining elements.
#include <iostream> #include <set> int main() { // Create a set and insert values into it std::set<int> mySet = {10, 20, 30, 40, 50}; // Display the set before deletion std::cout << "Set before deletion: "; for (int num : mySet) { std::cout << num << " "; } std::cout << std::endl; // Use an iterator to find the element to be deleted std::set<int>::iterator it = mySet.find(40); // Find the element '40' if (it != mySet.end()) { // Check if the element exists mySet.erase(it); // Erase the element using the iterator } // Display the set after deletion std::cout << "Set after deletion of 40: "; for (int num : mySet) { std::cout << num << " "; } return 0; }
The output displays the set before and after deleting the element 40, showing the remaining elements.
Set before deletion: 10 20 30 40 50 Set after deletion of 40: 10 20 30 50
Time Complexity: O(n log n) for set creation and insertion, and O(log n) for finding and erasing an element.
Time Complexity: O(n) because the set stores n elements.
Conclusion
In this article, we covered two methods for deleting elements from a set in C++. The first method uses the erase() function with a value, and the second uses an iterator to find and remove the element. Both methods are efficient and useful for managing unique elements in a set.