std::flat_map::begin,(3)

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

✨ std::flat_map::begin ✨

hey bestie 🫢 so this is a page straight out of the C++ Standard Library docs (the version living over at http://cppreference.com), and it's all about the begin family of member functions on std::flat_map. It's giving "small page, big slay" energy, ngl πŸ’…


🌸 SYNOPSIS 🌸

iterator begin() noexcept;              (1) (since C++23)
const_iterator begin() const noexcept;  (2) (since C++23)
const_iterator cbegin() const noexcept; (3) (since C++23)

okay so what does it actually do? It returns an iterator to the first element of the flat_map. That's it. That's the whole vibe ✨

πŸ’€ lowkey important: if the flat_map is empty, the iterator you get back will be equal to end() β€” so like, don't go dereferencing it, bestie, it's not pointing at anything πŸ™ˆ

oh and there's a cute lil diagram that ships with this page β€” range-begin-end.svg πŸŽ€


πŸ¦‹ PARAMETERS πŸ¦‹

(none)

literally nothing. no arguments. she takes zero notes πŸ’…


⭐ RETURN VALUE ⭐

An iterator to the first element. Iconic. No cap πŸ”₯


πŸ‘‘ COMPLEXITY πŸ‘‘

Constant. Like, O(1) constant, she's fast 🧚 the way that this is genuinely just a pointer grab? obsessed πŸ’œ


🌈 EXAMPLE 🌈

// Run this code

#include <iostream>
#include <flat_map>

int main()
{
    std::flat_map<int, double> map{{4, 4.13}, {9, 9.24}, {1, 1.09}};

    for (auto it = map.cbegin(); it != map.cend(); ++it)
        std::cout << '[' << it->first << "] = " << it->second << '\n';

    // Unlike std::map's bidirectional iterators, the std::flat_map iterators
    // are random-access, so they can be used with the operator[]:
    auto it = map.cbegin();
    assert(it[0] == map[1]);
    assert(it[1] == map[4]);
    assert(it[2] == map[9]);
}

okay but the comment in there is the real main character moment πŸ‘€ std::map's iterators are bidirectional, but std::flat_map's iterators are random-access, so you can hit them with operator[] and index straight into them. Slay πŸ’…


πŸ“ OUTPUT πŸ“

[1] = 1.09
[4] = 4.13
[9] = 9.24

look at that sorted order, she ate and left no crumbs 😭πŸ”₯


πŸ’– SEE ALSO πŸ’–

  • end β€” returns an iterator to the end
  • cend β€” (public member function)
  • begin, cbegin β€” returns an iterator to the beginning of a container or array, (C++11), (C++14), (function template)

kay that's the whole page bestie, every last detail preserved, no skips, no cap 🫢 this one's cppreference.com, version 2024.06.10 β€” go forth and iterate responsibly βœ¨πŸ’œ

πŸ“¦ where this page comes from