VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-delete-multiple-elements-from-a-set-in-cpp/

⇱ How to Delete Multiple Elements from a Set in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Delete Multiple Elements from a Set in C++?

Last Updated : 23 Jul, 2025

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 60

Removing 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.


Output
Original 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
Article Tags: