Open In App

deque crbegin in C++ STL

Last Updated : 27 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

deque::crbegin means constant_reverse_beginner and as the name suggests it returns a constant_reverse_iterator pointing to the last element of the deque.

What is a constant iterator?
A constant iterator is not used for modification. It is only used for accessing the elements. You can use non_const iterators for modifying the elements.

Syntax:

dequename.crbegin()

Return Value: It returns a const_reverse_iterator to the reverse beginning of the sequence.

Application:

Given a deque with numbers in the increasing order, print them in non increasing order.
Input: deque{1, 2, 3, 4, 5, 6};
for (auto reverseit = deque.crbegin(); reverseit != deque.crend(); ++reverseit)
cout << ' ' << *reverseit;

Output : 6 5 4 3 2 1

Below program illustrates the working of crbegin function:




// deque::crbegin and crend
#include <deque>
#include <iostream>
using namespace std;
int main()
{
    // Declare a deque with the name numdeque
    deque<int> numdeque = { 1, 2, 3, 4, 5, 6 };
  
    // Print the deque backwards using crbegin and crend
    cout << "Printing the numdeque backwards:";
  
    for (auto rit = numdeque.crbegin(); rit != numdeque.crend(); ++rit)
        cout << ' ' << *rit;
  
    return 0;
}


Output:

Printing the numdeque backwards: 6 5 4 3 2 1

Since the returned iterator is constant, if we try to change value, we get compiler error.




// deque::crbegin and crend
#include <deque>
#include <iostream>
using namespace std;
int main()
{
    // Declare a deque with the name numdeque
    deque<int> numdeque = { 1, 2, 3, 4, 5, 6 };
  
    // Print the deque backwards using crbegin and crend
    cout << "Printing the numdeque backwards:";
  
    for (auto rit = numdeque.crbegin(); rit != numdeque.crend(); ++rit)
         *rit = 10;
  
    return 0;
}


Output:

prog.cpp: In function ‘int main()’:
prog.cpp:14:8: error: assignment of read-only location ‘rit.std::reverse_iterator<_Iterator>::operator* >()’
*rit = 10;
     ^



Similar Reads

deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::pop_front() pop_front()
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
4 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
deque::emplace_front() and deque::emplace_back() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::emplace_front()This fun
5 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::front()fron
4 min read
Deque::empty() and deque::size() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::empty() emp
4 min read
multimap::crbegin() and multimap::crend() in C++ STL
multimap::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator referring to the last element in the multimap container. Since multimap container contains the element in an ordered way, crbegin() will point to that element that will come last according to the container's sorting criterion. Syntax: multimap_name.crbeg
3 min read
map crbegin() and crend() function in C++ STL
map::crbegin() is a built-in function in C++ STL that returns a constant reverse iterator referring to the last element in the map container. Since map container contains the element in an ordered way, crbegin() will point to that element that will come last according to the container's sorting criterion. Syntax: map_name.crbegin()Parameters: The f
3 min read
Practice Tags :