std::multimap::begin,(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿ’– std::multimap::begin ๐Ÿ’–

okay bestie, welcome to the C++ Standard Libary manual page for std::multimap::begin โ€” yes, the header spells it โ€œLibary,โ€ and we are keeping the typo energy. The page title is std::multimap::begin,(3), and this is literally the function that hands you an iterator to the first element of a multimap. โœจ

Empty multimap? Then the returned iterator will be equal to end(). Thatโ€™s the rule, no cap. ๐Ÿ’€

โœจ NAME โœจ

std::multimap::begin, โ€” the NAME line is literally std::multimap::begin, - std::multimap::begin, and honestly? Duplicate-comma main character moment. Weโ€™re not fixing it, weโ€™re respecting it. ๐Ÿ’…

๐ŸŒธ Synopsis ๐ŸŒธ

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

Returns an iterator to the first element of the multimap.

If the multimap is empty, the returned iterator will be equal to end().

Thereโ€™s also a diagram reference in the original page: range-begin-end.svg ๐Ÿ–ผ๏ธโœจ โ€” not me inventing lore, itโ€™s literally in there.

๐Ÿงธ Parameters

(none) โ€” literally no parameters. Not a single one, bestie. ๐Ÿซถ

๐ŸŽ€ Return value

Iterator to the first element. Thatโ€™s the whole gift. ๐Ÿ’

โšก Complexity

Constant. Thatโ€™s it, thatโ€™s the complexity. โšก

๐Ÿ“ Notes

libc++ backports cbegin() to C++98 mode. ๐Ÿ•ฐ๏ธโœจ ngl, a little time-travel moment.

๐Ÿ’ป Example

Hereโ€™s the example, preserved exactly โ€” donโ€™t come for me, Iโ€™m not touching the code:

       // Run this code

        #include <algorithm>
        #include <cassert>
        #include <cstddef>
        #include <iostream>
        #include <map>
        #include <string>

        int main()
        {
            auto show_node = [](const auto& node, char ending = '\n')
            {
                std::cout << "{ " << node.first << ", " << node.second << " }"
       << ending;
            };

            std::multimap<std::size_t, std::string> mmap;
            assert(mmap.begin() == mmap.end());   // OK
            assert(mmap.cbegin() == mmap.cend()); // OK

            mmap.insert({ sizeof(long), "LONG" });
            show_node(*(mmap.cbegin()));
            assert(mmap.begin() != mmap.end());   // OK
            assert(mmap.cbegin() != mmap.cend()); // OK
            mmap.begin()->second = "long";
            show_node(*(mmap.cbegin()));

            mmap.insert({ sizeof(int), "int" });
            show_node(*mmap.cbegin());

            mmap.insert({ sizeof(short), "short" });
            show_node(*mmap.cbegin());

            mmap.insert({ sizeof(char), "char" });
            show_node(*mmap.cbegin());

            mmap.insert({{ sizeof(float), "float" }, { sizeof(double),
       "double"}});

            std::cout << "mmap = { ";
            std::for_each(mmap.cbegin(), mmap.cend(), [&](const auto& n) {
       show_node(n, ' '); });
            std::cout << "};\n";
        }

๐Ÿ”ฎ Possible output

        { 8, LONG }
        { 8, long }
        { 4, int }
        { 2, short }
        { 1, char }
        mmap = { { 1, char } { 2, short } { 4, int } { 4, float } { 8, long } {
       8, double } };

๐Ÿ‘€ See also

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

okay thatโ€™s the page, bestie. Source/version: http://cppreference.com, dated 2024.06.10 โ€” manual footer was std::multimap::begin,(3) and we are sending it off with a ๐Ÿซถ. Byeee! ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from