Skip to content
Related Articles

Related Articles

Improve Article

deque crend in C++ STL

  • Last Updated : 19 Dec, 2018

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;
^

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :