C++ .empty()

cheetah3051's avatar
Published Feb 19, 2026

The .empty() method checks whether an unordered_set has no elements. It returns true if the container is empty (i.e., its size is 0) and false otherwise.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

unordered_set_name.empty()

Parameters:

This method does not take any parameters.

Return value:

Returns true if the unordered_set is empty and false otherwise.

Example

The following example demonstrates how to use the .empty() method with std::unordered_set in C++:

#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> numbers;
if (numbers.empty()) {
std::cout << "Unordered set is empty\n";
} else {
std::cout << "Unordered set has elements\n";
}
numbers.insert(10);
numbers.insert(20);
numbers.insert(30);
if (numbers.empty()) {
std::cout << "Unordered set is empty\n";
} else {
std::cout << "Unordered set has elements\n";
}
return 0;
}

The output of the above code is:

Unordered set is empty
Unordered set has elements

Codebyte Example

In this example, the .empty() method is used to control a loop that processes and removes elements from an unordered_set until it becomes empty:

Code
Output

All contributors

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours