In C++, std::array::rbegin() returns a reverse iterator. It starts on the last element, and ++ moves it toward the first element.
Because it is not const, you can also change elements through it. Use it with rend() to walk the array backwards. Include <array> to use it.
This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.
Syntax
Call rbegin with empty brackets. It takes no arguments.
C++
auto it = nums.rbegin();
*it = 50;
Call
What it does
nums.rbegin()
Returns a reverse iterator that points to the last element.
*(nums.rbegin() + 1)
Reads the second element from the end.
If you only need to read, crbegin() is the read-only version.
nums.rbegin() + 1 moves one place toward the front.
That place holds 30, so reverse iterators make "counting from the end" easy.
Frequently Asked Questions
It returns a reverse iterator that points to the last element. Using ++ on it moves toward the first element.
back() returns the last element itself. rbegin() returns an iterator that starts at the last element and can walk through the whole array backwards.
Yes, on a non-const array. *nums.rbegin() = 5; changes the last element.
No. Use ++. A reverse iterator already moves in the backward direction when you add.
They point to the same element, but rbegin() is a reverse iterator. It walks backwards with ++, while end() - 1 is a normal iterator that walks forward.