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:
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.
#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.
#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.
#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.