std::flat_multimap::empty(3)

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

πŸ’… std::flat_multimap::empty(3) β€” C++ Standard Library, bestie edition

okay so real talk, this page is from the C++ Standard Library manual (section 3, bestie, we're in the big leagues ✨) and it's literally about one tiny little function that answers one tiny little question: is my flat_multimap empty or not? that's it. that's the whole vibe check. 🫢


🌸 NAME 🌸

std::flat_multimap::empty β€” and yes bestie, the name of the function is literally std::flat_multimap::empty, no notes, iconic behavior, it does exactly what it says on the tin πŸ’…


✨ Synopsis ✨

here's the signature, copy it exactly, do not freestyle it:

[[nodiscard]] bool empty() const noexcept;  (since C++23)

so what does it do? it checks if the underlying containers have no elements. like, zero. nada. empty empty. πŸ•³οΈ

and it's equivalent to this:

return begin()
== end();

(yeah the original page line-wraps it like that, it's giving "my code got squished by the terminal" energy 😭 but that's the equivalence: return begin() == end();)

also note that [[nodiscard]] β€” the compiler will side-eye you HARD if you call this and just throw away the answer. don't do that. it's rude. πŸ’€

πŸŽ€ Parameters πŸŽ€

(none)

zero parameters. no args, no drama, she takes nothing and she gives everything. πŸ‘‘


πŸ¦‹ Return value πŸ¦‹

  • true if the underlying containers are empty
  • false otherwise

that's it. binary. black and white. no nuance, no "it depends", no <italic>maybe</italic>. 😌


🌈 Complexity 🌈

Constant. ⚑

like, instant. no loops, no scanning, no "let me think about it". just vibes and a comparison. ate and left no crumbs. πŸ”₯


πŸ“ Example πŸ“

the original page says: the following code uses empty to check if a std::flat_multimap<int, int> contains any elements. and honestly? great use of the function, 10/10, no notes.

// Run this code

#include <iostream>
#include <flat_map>
#include <utility>

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

bestie look at those numbers though β€” 42, 13, 13317, 123 β€” someone was having fun in the test suite and i respect it πŸ˜­πŸ’–

and notice the std::boolalpha β€” that's the little sprinkle that makes true/false print as actual words instead of 1/0. we love a readable output. ✨


🧚 Output 🧚

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

she starts empty, she gets some elements shoved in via emplace and insert, and then she's not empty anymore. character development. πŸ‘‘


πŸ’œ See also πŸ’œ

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

iykyk, size is the "how many" girlie and empty is the "any at all?" girlie. two different questions, both valid, both slaying. πŸ’…


ngl if you're calling empty() and then immediately calling size() to check if it's 0... bestie. that's the same question twice. pick a lane. 🫢

and that's the page!! source is cppreference.com, dated 2024.06.10 β€” go drink some water, go compile something with -std=c++23, and remember: [[nodiscard]] is not a suggestion, it's a lifestyle. mwah πŸ’‹βœ¨πŸŽ€

πŸ“¦ where this page comes from