std::array rend()/crend() method
- since C++17
- until C++17
// Non-const version
constexpr iterator rend() noexcept;
// Const version
constexpr const_iterator rend() const noexcept;
constexpr const_iterator crend() const noexcept;
// Non-const version
iterator rend() noexcept;
// Const version
const_iterator rend() const noexcept;
const_iterator crend() const noexcept;
It corresponds to the last element of the non-reversed array.
Returns a reverse iterator
to the last element of the reversed array. It corresponds to the element preceding the first element of the non-reversed array.It effectively returns an iterator that points past the end of the original array. Attempting to dereference a past-the-end iterator is undefined behaviour
.This method doesn't actually reverse the array, it just returns an iterator that points to the element before the first element of the array,
and whose +
, -
, --
, ++
operators have slightly changed implementations.
For example it++
decrements the internal pointer and it--
increments it (so that traversing the container in a reverse order actually works).
If the container is empty, the returned iterator will be equal to rbegin()
.
Parameters
(none)
Return value
Reverse iterator to the element before the first element.
Complexity
Constant.
Why before the first element?
This section requires improvement. You can help by editing this doc page.
Difference between rend and crend
For a const container c
, rend and crend are the same - c.rend() == c.crend()
For non-const container of type c
they return different iterators:
- Non const container
- Const container
- rend
- crend
#include <array>
int main()
{
std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.rend(); // Type: std::array<int, 5>::reverse_iterator
*std::prev(it) = 5; // ✔ Ok
}
#include <array>
int main()
{
std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.crend(); // Type: std::array<int, 5>::reverse_const_iterator
// ❌ Error!
*std::prev(it) = 5;
}
- rend
- crend
#include <array>
int main()
{
const std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.rend(); // Type: std::array<int, 5>::reverse_const_iterator
// ❌ Error!
*std::prev(it) = 5;
}
#include <array>
int main()
{
const std::array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.crend(); // Type: std::array<int, 5>::reverse_const_iterator
// ❌ Error!
*std::prev(it) = 5;
}
Example
#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';
}
40 69 76 76 79 12 0 35 11 11 1
Hello, C++!
Hover to see the original license.