std::array:...ray::cbegin(3)

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

πŸ’… std::array::begin, std::array::cbegin β€” std::array:...ray::cbegin(3) / C++ Standard Libary πŸ’…

okay bestie, welcome to the manual page for std::array::begin and std::array::cbegin β€” the littlest iterator-getters that are absolutely doing main character energy in the C++ Standard Libary. Yes, the manual says β€œLibary.” We’re keeping it. Periodt. πŸ’–


✨ NAME ✨

std::array::begin, std::array::cbegin β€” literally the page name, bestie: std::array::begin,std::array::cbegin. The dash repeats it too: std::array::begin,std::array::cbegin. It’s giving β€œI said what I said.” πŸ’…


🌸 Synopsis 🌸

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

So there are three overloads, numbered (1), (2), and (3), all since C++11, all noexcept, and all constexpr since C++17. Slay. ✨

Returns an iterator to the first element of the array.

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

There’s also a little visual in the original: range-begin-end.svg. It’s giving β€œrange-begin-end diagram” and we respect it. πŸ¦‹


πŸŽ€ Parameters πŸŽ€

(none) β€” literally no parameters, not a single one, no cap. πŸ™ˆ


πŸ’œ Return value πŸ’œ

Iterator to the first element. That’s the whole vibe. 🫢


⭐ Complexity ⭐

Constant. Not linear, not quadratic, just constant. Iconic. πŸ‘‘


πŸ”₯ Example πŸ”₯

       // Run this code

        #include <algorithm>
        #include <array>
        #include <iomanip>
        #include <iostream>

        int main()
        {
            std::cout << std::boolalpha;

            std::array<int, 0> empty;
            std::cout << "1) "
                      << (empty.begin() == empty.end()) << ' '     // true
                      << (empty.cbegin() == empty.cend()) << '\n'; // true
            // *(empty.begin()) = 42; // => undefined behavior at run-time

            std::array<int, 4> numbers{5, 2, 3, 4};
            std::cout << "2) "
                      << (numbers.begin() == numbers.end()) << ' '    // false
                      << (numbers.cbegin() == numbers.cend()) << '\n' // false
                      << "3) "
                      << *(numbers.begin()) << ' '    // 5
                      << *(numbers.cbegin()) << '\n'; // 5

            *numbers.begin() = 1;
            std::cout << "4) " << *(numbers.begin()) << '\n'; // 1
            // *(numbers.cbegin()) = 42; // compile-time error:
                                         // read-only variable is not
       assignable

            // print out all elements
            std::cout << "5) ";
            std::for_each(numbers.cbegin(), numbers.cend(), [](int x)
            {
                std::cout << x << ' ';
            });
            std::cout << '\n';

            constexpr std::array constants{'A', 'B', 'C'};
            static_assert(constants.begin() != constants.end());   // OK
            static_assert(constants.cbegin() != constants.cend()); // OK
            static_assert(*constants.begin() == 'A');              // OK
            static_assert(*constants.cbegin() == 'A');             // OK
            // *constants.begin() = 'Z'; // compile-time error:
                                         // read-only variable is not
       assignable
        }
bestie, those comments are part of the code and they are staying. Especially the β€œundefined behavior at run-time” and β€œcompile-time error” ones. We do not edit the sacred comments. πŸ’€

πŸ“ Output πŸ“

        1) true true
        2) false false
        3) 5 5
        4) 1
        5) 1 2 3 4

Ate. 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) β€” (function template)
  • (C++14)

That’s the whole see-also situation, and yes, every line is here. iykyk. 🧚


πŸ’– source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::array:...ray::cbegin(3) β€” bye bestie, go slay some iterators! πŸ«ΆπŸ’…

πŸ“¦ where this page comes from