How to Remove an Element from a Set in C++? Last Updated : 26 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, sets are a type of associative container in which each element has to be unique. The values are stored in a specific sorted order i.e. either ascending or descending. In this article, we will see how to remove specific elements from a set in C++. Example Input: set = {100,120,12,56,78,9,32,45,78,44}Deleting element = 56Output:{100,120,12,78,9,32,45,78,44}Delete a Specific Element from a Set in C++To delete a specific element from a set in C++, we can use the std::set::erase() function that is able to remove elements by using the value or iterator. C++ Program to Remove an Element from a Set C++ // C++ program to remove specific element from a set #include <iostream> #include <set> using namespace std; // Driver Code int main() { // Create Set with 10 integers set<int> set = { 100, 120, 12, 56, 78, 9, 32, 45, 78, 44 }; cout << "Existing Set" << endl; for (auto element : set) { cout << element << " "; } // Remove 56 from the set using the erase() function set.erase(56); cout << endl; cout << "Final Set" << endl; for (auto element : set) { cout << element << " "; } return 0; } OutputExisting Set 9 12 32 44 45 56 78 100 120 Final Set 9 12 32 44 45 78 100 120 Time complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Remove an Element from Vector in C++? S sravankumar_171fa07058 Follow Improve Article Tags : C++ Programs C++ cpp-set CPP Examples Practice Tags : CPP Similar Reads How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem 2 min read How to Remove an Element from Vector in C++? In this article, we will learn how to remove a given element from the vector in C++.The most straightforward method to delete a particular element from a vector is to use the vector erase() method. Let's look at a simple example that shows how to use this function:C++#include <bits/stdc++.h> u 2 min read How to Remove an Element from Array in C++? In C++, removing an element from an array is tricky because we cannot modify the memory space occupied by the array. So, we pseudo delete the element by moving it to the end of the array and reducing the size. In this article, we will learn how to remove a value from an array in C++.ExamplesInput: a 3 min read How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A 2 min read How to Remove an Element from the End of a List in C++? In C++, lists are data structures that allow us to store data of the same type in non-contiguous memory locations. In this article, we will learn how to remove an element from the end of a list in C++. Example Input: myList={10,20,30,40,50} Output: List Elements: 10 20 30 40Delete the Last Element f 2 min read How to Delete an Element from a Set in C++? 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 elem 2 min read Like