set::clear in C++ STL Last Updated : 22 Jan, 2018 Comments Improve Suggest changes 7 Likes Like Report 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. CPP // INTEGER SET // CPP program to illustrate // Implementation of clear() function #include <iostream> #include <set> using namespace std; int main() { set<int> myset{ 1, 2, 3, 4, 5 }; myset.clear(); // Set becomes empty // Printing the Set for (auto it = myset.begin(); it != myset.end(); ++it) cout << ' ' << *it; return 0; } Output: No Output CPP // CHARACTER SET // CPP program to illustrate // Implementation of clear() function #include <iostream> #include <set> using namespace std; int main() { set<char> myset{ 'A', 'b', 'd', 'e' }; myset.clear(); // Set becomes empty // Printing the Set for (auto it = myset.begin(); it != myset.end(); ++it) cout << ' ' << *it; return 0; } Output: No Output Time complexity: Linear i.e. O(n) Comment A AyushSaxena Follow 7 Improve A AyushSaxena Follow 7 Improve Article Tags : Misc C++ STL cpp-set Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like