std::array:...rray::crend(3)
π 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 beginningcrbeginβ (public member function)rendβ returns a reverse end iterator for a container or arraycrendβ (function template) (C++14)
ngl the whole point of this page is:rend/crendare 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
- 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