Przejdź do głównej zawartości

std::unordered_map empty() method

// Const version only
bool empty() const noexcept;

// Non const version only
[[nodiscard]] bool empty() const noexcept;

Checks if the container has no elements, i.e. whether begin() == end().

Parameters

(none)

Return value

true if the container is empty, false otherwise.

Complexity

Constant - O(1).

Why [[nodiscard]]?

The [[nodiscard]] attribute is an attribute that invokes compiler warnings whenever a function has been called and it's result has been discarded.

The reason behind the nodiscard being applied only to the empty method is that it's likely that the programmer might confuse the adjective empty ( which would mean - is this container empty?) for the verb empty (which would mean - please empty this container for me.).

Exceptions

(none)

Example

Main.cpp
#include <unordered_map>
#include <iostream>
#include <utility>

int main()
{
std::unordered_map<int, int> numbers;
std::cout << std::boolalpha;
std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';

numbers.emplace(42, 13);
numbers.insert(std::make_pair(13317, 123));
std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
}
Output
Initially, numbers.empty(): true
After adding elements, numbers.empty(): false
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::unordered_map empty() method

// Const version only
bool empty() const noexcept;

// Non const version only
[[nodiscard]] bool empty() const noexcept;

Checks if the container has no elements, i.e. whether begin() == end().

Parameters

(none)

Return value

true if the container is empty, false otherwise.

Complexity

Constant - O(1).

Why [[nodiscard]]?

The [[nodiscard]] attribute is an attribute that invokes compiler warnings whenever a function has been called and it's result has been discarded.

The reason behind the nodiscard being applied only to the empty method is that it's likely that the programmer might confuse the adjective empty ( which would mean - is this container empty?) for the verb empty (which would mean - please empty this container for me.).

Exceptions

(none)

Example

Main.cpp
#include <unordered_map>
#include <iostream>
#include <utility>

int main()
{
std::unordered_map<int, int> numbers;
std::cout << std::boolalpha;
std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';

numbers.emplace(42, 13);
numbers.insert(std::make_pair(13317, 123));
std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
}
Output
Initially, numbers.empty(): true
After adding elements, numbers.empty(): false
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.