std::map::empty(3)

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

✨ std::map::empty(3) ✨

okay so welcome to the std::map::empty(3) page from the C++ Standard Libary manual β€” yes bestie, we're checking whether a std::map is empty, and this is literally the whole vibe.


🌸 NAME 🌸

std::map::empty - std::map::empty β€” that's the name, no cap.


🌸 SYNOPSIS 🌸

here are the signature glow-ups, and yes the until / since labels matter:

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

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


πŸ’– PARAMETERS πŸ’–

none β€” literally no parameters. she takes nothing.


πŸŽ€ RETURN VALUE πŸŽ€

true if the container is empty, false otherwise.


πŸ¦‹ COMPLEXITY πŸ¦‹

Constant.


✨ EXAMPLE ✨

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

Run this code:

#include <iostream>
#include <map>
#include <utility>

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

🌈 SEE ALSO 🌈

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

xoxo, your Unix bestie πŸ’– β€” source/version: http://cppreference.com; date: 2024.06.10.

πŸ“¦ where this page comes from