C++ Unordered_set Library - empty


Advertisements

Description

It returns a bool value indicating whether the unordered_set container is empty, i.e. whether its size is 0.

Declaration

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

C++11

bool empty() const noexcept;

Parameters

none

Return value

It returns true if the container size is 0, false otherwise.

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::empty.

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

int main () {
   std::unordered_set<std::string> first = {"sairam","krishna","mammahe"};
   std::unordered_set<std::string> second;
   std::cout << "first " << (first.empty() ? "is empty" : "is not empty" ) << std::endl;
   std::cout << "second " << (second.empty() ? "is empty" : "is not empty" ) << std::endl;
   return 0;
}

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

first is not empty
second is empty
unordered_set.htm
Advertisements