std::array:...rray::crend(3)

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

πŸŽ€ NAME πŸŽ€

std::array::rend, std::array::crend β€” ok so this is the whole page, bestie: it's giving "the two end-iterators of the reverse world" and it's from the C++ Standard Libary (yes that's how the manual spells it, i don't make the rules πŸ’…).


✨ Synopsis ✨

reverse_iterator rend() noexcept;               (1) (since C++11)
                                                    (constexpr since C++17)
const_reverse_iterator rend() const noexcept;   (2) (since C++11)
                                                    (constexpr since C++17)
const_reverse_iterator crend() const noexcept;  (3) (since C++11)
                                                    (constexpr since C++17)

So there's three flavors, numbered (1), (2), (3) up there ☝️ β€” the plain rend(), the const rend(), and crend(). All of them are noexcept (no throwing, we behave ✨), all of them landed in C++11, and all of them got upgraded to constexpr in C++17 (compile-time era, we love to see it 🫢).

🌸 What it actually does

It hands you a reverse iterator pointing at the element following the last element of the reversed array. Which β€” plot twist β€” is the same slot as the element preceding the first element of the non-reversed array. πŸ”

That element is literally just a placeholder, okay? It's the understudy. The seat filler. Attempting to access it results in undefined behavior β€” so like, do NOT dereference it, i'm so serious πŸ˜­πŸ’€. It's the "end" marker, not a real value. Respect the boundary.

There's also a lil diagram floating around named range-rbegin-rend.svg that shows the whole range vibe β€” iykyk.


πŸ¦‹ Parameters πŸ¦‹

  • (none) β€” nothing to pass, she takes no arguments, she's low maintenance πŸ’…

⭐ Return value ⭐

A reverse iterator to the element following the last element. That's the whole deliverable, no cap.


🧚 Complexity 🧚

Constant. O(1) energy β€” instant, iconic, no waiting around 🌈


πŸ”₯ Example πŸ”₯

// Run this code

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

int main()
{
    std::array<int, 11> a{1, 11, 11, 35, 0, 12, 79, 76, 76, 69, 40};

    // print elements of array in reverse order using const_reverse_iterator`s
    std::for_each(a.crbegin(), a.crend(), [](int e){ std::cout << e << ' '; });
    //              ^^           ^^
    std::cout << '\n';

    // modify each element of array using non-const reverse_iterator`s
    std::for_each(a.rbegin(), a.rend(), [](int& e){ e += 32; });
    //              ^           ^             ^

    // print elements as chars in reverse order using const_reverse_iterator`s
    std::for_each(a.crbegin(), a.crend(), [](char e){ std::cout << e; });
    //              ^^           ^^          ^^^^
    std::cout << '\n';
}

Notice how the first pass uses crbegin()/crend() β€” read-only, can't touch anything, just vibing πŸ‘€ β€” then the second pass uses rbegin()/rend() with int& so it can actually mutate every element (e += 32; β€” iconic math, adds 32 to each one, which is exactly the offset that turns numbers into letters, sneaky πŸ“), and then a third read-only pass re-reads them as char so the array spells something out. Main character energy, honestly.

Output

40 69 76 76 79 12 0 35 11 11 1
Hello, C++!

The way that second line just happens to say hi πŸ˜­πŸ’–


πŸ‘‘ See also πŸ‘‘

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

ngl the whole point of this page is: rend/crend are the "one past the end, but backwards" markers, and you never ever dereference them. That's it, that's the tea πŸŽ€

okay bye bestie, go iterate in reverse responsibly πŸ’œ

source: http://cppreference.com β€” version date 2024.06.10 ✨🫢

πŸ“¦ where this page comes from