How to Delete Multiple Elements from a Set in C++? Last Updated : 18 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Multiple Elements from a Set in C++We can delete multiple elements from a set in C++ by passing each value to the std::set::erase() member function of the std::set class. We can use the loop for deleting multiple values. C++ Program to Delete Multiple Elements from a Set The below example demonstrates how we can use erase() function to delete multiple elements from a set in C++ STL. C++ // C++ Program to illustrate how to delete multiple elements // from a set #include <iostream> #include <set> using namespace std; int main() { // Set and elements to be deleted from a set set<int> s = { 10, 20, 30, 40, 50, 60 }; set<int> elements_to_delete = { 20, 30, 40 }; // Print the original set cout << "Original set: "; for (auto it = s.begin(); it != s.end(); ++it) cout << *it << " "; cout << endl; // Delete elements from the set for (auto it = elements_to_delete.begin(); it != elements_to_delete.end(); ++it) s.erase(*it); // Print the set after deletion cout << "Set after deletion: "; for (auto it = s.begin(); it != s.end(); ++it) cout << *it << " "; cout << endl; return 0; } OutputOriginal set: 10 20 30 40 50 60 Set after deletion: 10 50 60 Time Complexity: O(K log N), here K is the number of elements to delete and N is the size of the original set.Auxilliary Space: O(N) Comment More infoAdvertise with us Next Article How to Delete an Element from a Set in C++? B bug8wdqo Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Delete Multiple Elements from a Vector in C++? In this article, we will learn how to erase multiple elements from a vector in C++.The easiest and most efficient method to delete the multiple elements from the vector is by using vector erase() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main( 2 min read 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 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 the Last Element from a Multiset in C++? In C++, a multiset is a container that can store multiple elements, including duplicates. In this article, we will learn how to delete the last element from a multiset in C++. Example: Input:myMultiset = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }Element to delete = 4Output: myMultiset = { 1, 2, 2, 3, 3, 3, 4, 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 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 Like