C++ std::array::crbegin
The std::array::crbegin
function returns a constant reverse iterator pointing to the last element of the array. This iterator allows read-only access to the array elements in reverse order. It is commonly used when you need to traverse a const
array or enforce non-modifiable access while iterating in reverse.
Syntax of std::array::crbegin
const_reverse_iterator crbegin() const noexcept;
Parameters
The std::array::crbegin
function does not take any parameters.
Return Value
Returns a constant reverse iterator pointing to the last element of the array, which is the first element in reverse order. If the array is empty, the returned iterator is equal to std::array::crend
.
Examples for std::array::crbegin
Example 1: Iterating Through an Array in Reverse Using std::array::crbegin
This example demonstrates iterating through the elements of a std::array
in reverse order using crbegin()
and crend()
:
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {10, 20, 30, 40, 50};
std::cout << "Array elements in reverse order: ";
for (auto it = arr.crbegin(); it != arr.crend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Define an array: A
std::array
of size 5 is initialized with elements{10, 20, 30, 40, 50}
. - Use
crbegin()
andcrend()
: Thecrbegin()
function provides a constant reverse iterator pointing to the last element, whilecrend()
points to one before the first element of the array in reverse order. - Iterate in reverse: A for loop is used to traverse the array in reverse order. The elements are accessed using the dereference operator
*it
, and the constant reverse iterator ensures the array elements remain unmodified.
Output:
Array elements in reverse order: 50 40 30 20 10
Example 2: Using std::array::crbegin with a Standard Algorithm
This example demonstrates using std::array::crbegin
with the std::for_each
algorithm to print the elements of a std::array
in reverse order:
#include <iostream>
#include <array>
#include <algorithm>
int main() {
std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "Array elements in reverse order: ";
std::for_each(arr.crbegin(), arr.crend(), [](int x) {
std::cout << x << " ";
});
std::cout << std::endl;
return 0;
}
Explanation:
- Original array: The array
arr
is initialized with elements{1, 2, 3, 4, 5}
. - Use
std::for_each
: Thestd::for_each
algorithm is applied to the range defined bycrbegin()
andcrend()
. A lambda function is used to print each element. - Traverse in reverse: The
crbegin()
iterator starts at the last element, and the iteration ends before the first element as indicated bycrend()
. - Output the result: The lambda function ensures each element is printed in reverse order.
Output:
Array elements in reverse order: 5 4 3 2 1
Exception Handling in std::array::crbegin
The std::array::crbegin
function does not throw exceptions as it is marked noexcept
. This ensures safe access to a constant reverse iterator pointing to the last element of the array.