The C++ std::array::crend() function is used to return a constant reverse iterator pointing to the element preceding the first element of the array. This function is used to traverse the array in reverse order without modifying its element.
SyntaxThe crend() function is typically used in conjunction with crbegin() to create a reverse iteration range.
Following is the syntax for std::array::crend() function.
const_reverse_iterator crend() const noexcept;Parameters
None
Return ValueIt returns a reverse constant iterator pointing to the past-end element of the array.
ExceptionsThis function never throws exception.
Time complexityConstant i.e. O(1)
Example 1In the following example, we are going to consider the basic usage of the crend() function.
#include <iostream> #include <array> using namespace std; int main(void) { array < int, 5 > arr = {10,20,30,40,50}; auto s = arr.crbegin(); auto e = arr.crend(); while (s < e) { cout << * s << " "; ++s; } cout << endl; return 0; }Output
Output of the above code is as follows −
50 40 30 20 10Example 2
Consider the following example, where we are going to apply the crend() function on the string array.
#include <iostream> #include <array> using namespace std; int main() { array < string, 3 > MyArray {"Tutorials","point","company"}; array < string, 3 > ::const_reverse_iterator crit; crit = MyArray.crend(); crit--; cout << * crit << " "; crit--; cout << * crit << " "; crit--; cout << * crit << " "; return 0; }Output
Following is the output of the above code −
Tutorials point company
array.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4