How to Delete an Element from a Set in C++? Last Updated : 26 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific element from a std::set in C++, we can use the std::set::erase() function. This function removes all elements with a certain value from the set. We can also pass it to the iterator if we need. C++ Program to Delete an Element from a Set in C++ C++ // CPP program to delete an element from a Set #include <iostream> #include <set> using namespace std; // Driver Code int main() { // Creating a set set<int> mySet = { 1, 2, 3, 4, 5 }; // Printing elements before deletion cout << "Elements before deletion: "; for (int elem : mySet) { cout << elem << " "; } cout << endl; // Deleting element using iterators auto it = mySet.find(3); if (it != mySet.end()) { mySet.erase(it); } // Printing elements after deletion cout << "Elements after deletion using iterators: "; for (int elem : mySet) { cout << elem << " "; } cout << endl; return 0; } // This code is contributed by Susobhan Akhuli OutputElements before deletion: 1 2 3 4 5 Elements after deletion using iterators: 1 2 4 5 Time Complexity: O(log N), where N is the number of elements in the set.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Delete All Elements from a Vector in C++? S susobhanakhuli19 Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Delete an Element from a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to delete a specific element from a multiset. Example: Input: myMultiset = {5, 2, 8, 5, 8, 8}; Element to delete: 8 Output: myMultiset = {5, 2, 5 2 min read How to Delete an Element from the Set in C++? In C++, the set container stores unique elements in the sorted order and the deletion operation should not disrupt this sorted order.C++ provides a built-in function set erase(), which can be used to easily delete the given element by passing its value to the function. Let's take a look at the simpl 1 min read How to delete last element from a set in C++ Given a Set, the task is to remove the last element from this Set in C++. Examples: Input: set = [10 20 30 70 80 90 100 40 50 60] Output: 10 20 30 40 50 60 70 80 90 Input: set = [1 2 3 4 5] Output: 1 2 3 4 Sets are a type of associative containers in which each element has to be unique because the v 3 min read How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element 3 min read How to Delete All Elements from a Vector in C++? In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Letâs take a look at a simple exa 2 min read How to Delete Multiple Elements from a Set in C++? In C++, sets are containers that store unique elements in some sorted order. In this article, we will learn how to delete multiple elements from a set using C++. Example: Input: set = {10, 20, 30, 40, 50, 60}elements_to_delete = {20, 30, 40}Output: Elements of set after deletion: 10 50 60Removing Mu 2 min read Like