std::unorde...iset::empty(3)

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

πŸ’… std::unordered_multiset::empty

okay bestie, welcome to the <unordered_set> glow-up page πŸ’– this one lives in the C++ Standard Library and the star of the show is std::unordered_multiset::empty β€” yes the name is literally the same as the page title, we love a self-titled icon ✨


✨ SYNOPSIS ✨

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

so basically it checks if the container has no elements, i.e. whether begin() == end() πŸŽ€ like, is your multiset giving empty-girl energy or not, that's the whole vibe check.

heads up: since C++20 it's got [[nodiscard]] on it β€” meaning if you call it and then just ignore the result, the compiler is gonna side-eye you 😭 don't do that, use the answer.

🌸 PARAMETERS 🌸

(none) β€” not a single argument, she takes zero notes, zero input, purely vibes 🫢


⭐ RETURN VALUE ⭐

  • true if the container is empty πŸŽ‰
  • false otherwise πŸ’€

πŸ¦‹ COMPLEXITY πŸ¦‹

Constant. β€” as in, O(1), instant, no scrolling required, iconic behavior πŸ‘‘


🌈 EXAMPLE 🌈

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

// Run this code

#include <iostream>
#include <unordered_set>

int main()
{
    std::unordered_multiset<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

see how it flipped from true to false once we inserted 42 and 19937?? the way that she just knows 😭πŸ”₯ main character energy fr fr.


πŸ‘€ SEE ALSO

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

and that's the whole page bestie, no cap, every last detail accounted for πŸ’…

source/version + date: this lil masterpiece comes from http://cppreference.com, version 2024.06.10 β€” mwah, catch you on the next manpage πŸ«ΆπŸ’–πŸŽ€
πŸ“¦ where this page comes from