Deque Crbegin Function - C++ Programming Tutorial
C++ Course / STL Queue & Stack / Deque Crbegin Function

Deque Crbegin Function

BLUF: Mastering Deque Crbegin Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Deque Crbegin Function

C++ is renowned for its efficiency. Learn how Deque Crbegin Function enables low-level control and high-performance computing in the tutorial below.

C++ Deque crbegin

The crbegin function in C++ for deques provides a constant reverse iterator that points to the final element in the deque. This iterator allows for traversal in reverse order but does not permit modification of the deque's contents.

Whereas, crbegin represents the constant reverse beginning iterator.

Syntax

Example

const_reverse_iterator crbegin();

Parameter

It does not contain any parameter.

Return value

It yields a constant reverse iterator that references the final element within the deque data structure.

Example 1

Let's see a simple example

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
   deque<int> i={10,20,30,40,50};
   deque<int>::const_reverse_iterator citr;
   for(citr=i.crbegin();citr!=i.crend();++citr)
   {
       cout<<*citr;
       cout<<" ";
   }
   return 0;}

Output:

Output

50 40 30 20 10

In this instance, the crbegin method is employed to retrieve an iterator pointing to the final element, and the for loop is executed until it reaches the initial element of the deque.

Example 2

Let's explore a basic scenario where an iterator is incremented.

Example

#include <iostream>
#include<deque>
using namespace std;
int main()
{
   deque<string> fruit={"electronics","computer science","mechanical","electrical"};
   deque<string>::const_reverse_iterator citr=fruit.crbegin()+1;
  cout<<*citr;
   return 0;
}

Output:

Output

mechanical

In this instance, the constant reverse iterator is advanced by one, allowing it to retrieve the second element from the end.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience