std::flat_map::begin,(3)
β¨ 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 theflat_mapis empty, the iterator you get back will be equal toend()β 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 endcendβ (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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms