Vector Crbegin Function - C++ Programming Tutorial
C++ Course / STL Vector / Vector Crbegin Function

Vector Crbegin Function

BLUF: Mastering Vector 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: Vector Crbegin Function

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

C++ Vector crbegin

The crbegin function represents the reverse beginning. It is employed to reference the final character of the vector container.

crbegin vs rbegin

The crbegin method provides a constant reverse iterator, whereas the rbegin function offers a regular reverse iterator. Elements accessed using the rbegin function are editable, unlike those accessed using the crbegin function.

Syntax

Consider a vector 'v'. Syntax would be:

Example

const_reverse_iterator itr=v.crbegin();

Parameter

It does not contain any parameter.

Return value

It provides a constant reverse iterator that points to the reverse start of the container.

Example 1

Let's see a simple example.

Example

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{100,200,300,400};
vector<int>::const_reverse_iterator itr=v.crbegin();
  *itr=500;
cout<<*itr;
return 0;}

Output:

In this instance, we are attempting to alter the value utilizing the crbegin function, which is not feasible in this scenario.

Example 2

Let's see another simple example.

Example

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"Mango","banana","strawberry","kiwi"};
vector<string>::const_reverse_iterator itr=v.crbegin();
cout<<*itr;
return 0;
}

Output:

In this instance, the crbegin method is employed to retrieve the final element of the vector container.

Example 3

Let's see a simple example.

Example

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
vector<int>::const_reverse_iterator itr=v.crbegin()+2;
cout<<*itr;
return 0;
}

Output:

In this instance, the crbegin method is advanced by 2 positions to retrieve the third element within the vector, allowing access to all elements in reverse order.

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