std::vector...or::crbegin(3)

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

okay bestie, this is the std::vector...or::crbegin(3) page from the C++ Standard Libary manual. yes, the header is a little cursed (vector...or), but the energy is immaculate β€” it’s giving reverse-iterator main character energy πŸ’…βœ¨

πŸ’– NAME πŸ’–

  • std::vector::rbegin,std::vector::crbegin β€” with the little - after it
  • std::vector::rbegin,std::vector::crbegin β€” yep, it says it again, and we’re keeping it

πŸ“œ Synopsis πŸ“œ

here’s the exact signature menu, annotations and all 🫢

          reverse_iterator rbegin();                       (1) (noexcept since
       C++11)
                                                               (constexpr since
       C++20)
          const_reverse_iterator rbegin() const;           (2) (noexcept since
       C++11)
                                                               (constexpr since
       C++20)
          const_reverse_iterator crbegin() const noexcept; (3) (since C++11)
                                                               (constexpr since
       C++20)

so what does it do? it returns a reverse iterator to the first element of the reversed vector. that maps to the last element of the non-reversed vector. if the vector is empty, the returned iterator is equal to rend().

there’s even a diagram file named range-rbegin-rend.svg β€” slay πŸ¦‹

πŸŽ€ Parameters πŸŽ€

(none) β€” literally none. no parameters, no stress 🫢

πŸ¦‹ Return value πŸ¦‹

Reverse iterator to the first element. that’s the whole return value, bestie.

🌸 Complexity 🌸

Constant. no scaling nonsense, just constant 🌈

✨ Notes ✨

the underlying iterator of the returned reverse iterator is the end iterator. hence the returned iterator is invalidated if and when the end iterator is invalidated. that’s a real caveat, okay? keep it in mind πŸ’€

also: libc++ backports crbegin() to C++98 mode. iconic behavior, ngl πŸ‘‘

πŸ’… Example πŸ’…

here’s the exact example, copied with love:

       // Run this code

        #include <algorithm>
        #include <iostream>
        #include <numeric>
        #include <string>
        #include <vector>

        int main()
        {
            std::vector<int> nums{1, 2, 4, 8, 16};
            std::vector<std::string> fruits{"orange", "apple", "raspberry"};
            std::vector<char> empty;

            // Print vector.
            std::for_each(nums.rbegin(), nums.rend(), [](const int n) {
       std::cout << n << ' '; });
            std::cout << '\n';

            // Sums all integers in the vector nums (if any), printing only the
       result.
            std::cout << "Sum of nums: "
                      << std::accumulate(nums.rbegin(), nums.rend(), 0) <<
       '\n';

            // Prints the first fruit in the vector fruits, checking if there
       is any.
            if (!fruits.empty())
                std::cout << "First fruit: " << *fruits.rbegin() << '\n';

            if (empty.rbegin() == empty.rend())
                std::cout << "vector 'empty' is indeed empty.\n";
        }

πŸ’€ Output πŸ’€

        16 8 4 2 1
        Sum of nums: 31
        First fruit: raspberry
        vector 'empty' is indeed empty.

πŸ‘‘ See also πŸ‘‘

  • rend β€” returns a reverse iterator to the end
  • crend β€” (public member function) (C++11)
  • rbegin β€” returns a reverse iterator to the beginning of a container or array
  • crbegin β€” (function template) (C++14)

byeeee bestie 🫢 this page’s source/version is http://cppreference.com, dated 2024.06.10, and the footer tag is std::vector...or::crbegin(3). mwah πŸ’–

πŸ“¦ where this page comes from