The deque::crend() is an inbuilt function in C++ STL which returns a constant reverse iterator which points to the position before the first element of the deque. Syntax
deque_name.crend()
Parameters: This function does not accept any parameters. Return Type: This function returns constant reverse iterator of deque. Example-1:
// C++ program to illustrate the
// deque::crend() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
deque<int> dq = { 10, 20, 30, 40, 50 };
cout << "The deque in reverse order: \n";
// prints the elements in reverse order
for (auto it = dq.crend() - 1; it >= dq.crbegin(); --it)
cout << *it << endl;
return 0;
}
Output:
The deque in reverse order: 10 20 30 40 50
Example-2: Since the iterator is constant, making an attempt to change it would cause error.
// C++ program to illustrate the
// deque::crend() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
deque<char> dq = { 'a', 'b', 'c', 'd', 'e', 'f' };
cout << "The deque in reverse order: \n";
// prints the elements in reverse order
for (auto it = dq.crend() - 1; it >= dq.crbegin(); --it)
*it = 'g'
return 0;
}
Compilation Error in CPP code :- prog.cpp: In function 'int main()': prog.cpp:15:13: error: assignment of read-only location 'it.std::reverse_iterator<_Iterator>::operator* >()' *it = 'g' ^ prog.cpp:17:5: error: expected ';' before 'return' return 0; ^
Time complexity: O(N). // N is the size of the deque.
Auxiliary space: O(1)