std::set::empty(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

std::set::empty - std::set::empty, bestie β€” yes, that's the whole name line, and it's giving β€œthe function is literally named after what it checks.” This page is std::set::empty(3) from the C++ Standard Libary manual (spelled exactly like that in the header, no cap πŸ‘€).


✨ Synopsis ✨

Okay so here are the exact signatures, era labels and all β€” keep that lone (until C++20) line, it's part of the package πŸŽ€:

bool empty() const;                         (until C++11)
bool empty() const noexcept;                (since C++11)
                                                      (until C++20)
[[nodiscard]] bool empty() const noexcept;  (since C++20)

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


🌸 Parameters 🌸

(none) β€” zero parameters, zero stress, it is literally not asking you for anything 🫢.


πŸ¦‹ Return value πŸ¦‹

  • true if the container is empty
  • false otherwise

That's it. Iconic behavior, no notes πŸ’….


⭐ Complexity ⭐

Constant. Lowkey the easiest complexity reveal ever.


πŸŽ€ Example πŸŽ€

The following code uses empty to check if a std::set<int> contains any elements:

// Run this code

#include <iostream>
#include <set>

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

    numbers.insert(42);
    numbers.insert(19937);
    std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
}

Output:

Initially, numbers.empty(): true
After adding elements, numbers.empty(): false

Not the numbers 42 and 19937 doing main character energy in the example 😭✨.


πŸ’… See also πŸ’…

  • size β€” returns the number of elements (public member function)
  • empty (C++17) β€” checks whether the container is empty (function template)

mwah πŸ’‹ source/version/date sign-off: http://cppreference.com Β· 2024.06.10 Β· std::set::empty(3) β€” bye bestie, go check those containers πŸ«ΆπŸ’–

πŸ“¦ where this page comes from