deque crend in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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



My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.