C++ Unordered_set Library - clear



Description

It is used to clear content.

Declaration

Following is the declaration for std::unordered_set::clear.

C++11

void clear() noexcept;

Parameters

none

Return value

none

Exceptions

Exception is thrown if any element comparison object throws exception.

Please note that invalid arguments cause undefined behavior.

Time complexity

constant time.

Example

The following example shows the usage of std::unordered_set::clear.

#include <iostream>
#include <string>
#include <unordered_set>

int main () {
   std::unordered_set<std::string> myset =
      { "sai", "ram", "krishna", "prasad" };

   std::cout << "myset contains:";
   for (const std::string& x: myset) std::cout << " " << x;
   std::cout << std::endl;

   myset.clear();
   myset.insert("Tutorialspoint");
   myset.insert("Technical Analyst");
   myset.insert("Hyderabad");

   std::cout << "myset contains:";
   for (const std::string& x: myset) std::cout << " " << x;
   std::cout << std::endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

myset contains: prasad krishna ram sai
myset contains: Hyderabad Technical Analyst Tutorialspoint
unordered_set.htm
Advertisements