deque crend in C++ STL
Last Updated :
20 Feb, 2023
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:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
deque< int > dq = { 10, 20, 30, 40, 50 };
cout << "The deque in reverse order: \n";
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.
CPP
#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";
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...