![]() |
VOOZH | about |
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
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.
The below example demonstrates how we can use erase() function to delete multiple elements from a set in C++ STL.
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)