Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.
set::clear()
clear() function is used to remove all the elements of the set container, thus making its size 0.
Syntax :
setname.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the set are
removed ( or destroyed )
Examples:
Input : set{1, 2, 3, 4, 5};
set.clear();
Output : set{}
Input : set{};
set.clear();
Output : set{}
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int > myset{ 1, 2, 3, 4, 5 };
myset.clear();
for ( auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
No Output
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< char > myset{ 'A' , 'b' , 'd' , 'e' };
myset.clear();
for ( auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
No Output
Time complexity: Linear i.e. O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jan, 2018
Like Article
Save Article