std::multiset::empty(3)

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

πŸ’… NAME πŸ’…

std::multiset::empty β€” bestie, it literally just tells you if your multiset is empty, that's it, that's the whole vibe ✨

This one lives in the C++ Standard Library man pages (std::multiset::empty(3)), so you already know it's giving iconic container energy πŸ‘‘


✨ Synopsis ✨

Watch the glow-up across the standards, because this signature has been through some eras πŸ¦‹

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

Ok so what does it actually do? It checks if the container has no elements, i.e. whether begin() == end(). That's the whole check, no cap 😌


πŸŽ€ Parameters πŸŽ€

(none)

Literally nothing. It takes zero arguments. It's a main character that needs no input πŸ’…


🌸 Return value 🌸

true if the container is empty, false otherwise.

Simple as that, bestie. Binary. Iconic. Periodt 🫢


πŸ¦‹ Complexity πŸ¦‹

Constant.

Constant!! It's O(1)! It does not care how big your multiset is, it's answering instantly, no thoughts head empty fr fr ⚑


πŸ’– Example πŸ’–

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

// Run this code

#include <iostream>
#include <set>

int main()
{
    std::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';
}

Not me getting emotional over a multiset going from empty to having two whole elements in it πŸ˜­πŸ’€


⭐ Output ⭐

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

Look at it. Look at the character development. Starts empty, ends full. We love a growth arc 🌈


πŸ‘‘ See also πŸ‘‘

  • size β€” returns the number of elements (public member function)
  • empty (C++17) β€” checks whether the container is empty (function template)
πŸ’‘ Quick vibe check: those two emptys are not the same thing β€” one is the member function you're reading about right now, and the other is the C++17 function template. Don't mix them up bestie πŸ™ˆ

and that's the whole page, you ate that πŸ’…

Source and version: http://cppreference.com, 2024.06.10 β€” and the page you just read was std::multiset::empty(3). Now go forth and check your containers with confidence, bestie πŸ“πŸ§šπŸ’œ

πŸ“¦ where this page comes from