English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and Example of Deque crbegin()

C++ Deque (Double-ended Queue)

C ++The crbegin() function of deque returns a constant reverse iterator that refers to the last element of the deque. The iterator can be incremented or decremented, but cannot modify the content of the deque.

Where, crbegin() indicates a constant reverse start.

Syntax

const_reverse_iterator crbegin();

Parameters

It does not contain any parameters.

Return value

It returns a constant reverse iterator that points to the last element in the deque container.

Example1

Let's look at a simple example

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

Output:

50 40 30 20 10

In this example, the crbegin() function is used to return an iterator to the last element of the deque, and the for loop is iterated until it reaches the first element of the deque.

Example2

Let's look at a simple example when the iterator is incremented.

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

Output:

mechanical

In this example, the constant reverse iterator is incremented1Therefore, it accesses the second element from the back.

C++ Deque (Double-ended Queue)