Przejdź do głównej zawartości

std::array rend()/crend() method

// Non-const version
constexpr iterator rend() noexcept;

// Const version
constexpr const_iterator rend() const noexcept;
constexpr 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

.

zanotuj

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?

important

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:

#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
}

Example

Main.cpp
#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';
}
Output
40 69 76 76 79 12 0 35 11 11 1 
Hello, C++!
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::array rend()/crend() method

// Non-const version
constexpr iterator rend() noexcept;

// Const version
constexpr const_iterator rend() const noexcept;
constexpr 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

.

zanotuj

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?

important

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:

#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
}

Example

Main.cpp
#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';
}
Output
40 69 76 76 79 12 0 35 11 11 1 
Hello, C++!
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.