The Wayback Machine - https://web.archive.org/web/20240527123724/https://www.geeksforgeeks.org/vectorcrend-vectorcrbegin-examples/
Open In App

vector::crend() & vector::crbegin() with example

Last Updated : 31 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

These functions return useful iterators to access vector elements in reverse order (from end to beginning)

Using vector::crend()

It’s a public member function that returns a const_reverse_iterator pointing to the element preceding the first element. 

Return Value

A const_reverse_iterator to the reverse 
end of the sequence.

Syntax:

const_reverse_iterator crend() const noexcept;

Time Complexity – constant O(1)

Example 1:

C++




#include <iostream> 
#include<vector> 
using namespace std; 
int main() 
{ 
vector<string>str{"avi","aniket","akash","ajay"}; 
vector<string>::const_reverse_iterator itr=str.crend()-1; 
std::cout<< *itr; 
return 0; 
}


Output

avi

Using vector::crbegin()

It returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning).. 

Return Value

A const_reverse_iterator to the reverse 
beginning of the sequence.

Syntax:

const_reverse_iterator crbegin() const noexcept;

Example 1 : 

C++




#include <iostream> 
#include<vector> 
using namespace std; 
int main() 
{ 
vector<string> v{"akash","ajay","aniket","avi"}; 
vector<string>::const_reverse_iterator itr=v.crbegin(); 
cout<<*itr; 
return 0; 
}


Output

avi

CPP




// CPP program to illustrate working of crbegin()
// crend()
#include <iostream>
#include <vector>
using namespace std;
 
int main ()
{
// initializing vector with values
vector<int> vect = {10, 20, 30, 40, 50};
 
// for loop with crbegin and crend
for (auto i = vect.crbegin(); i != vect.crend(); i++)
    cout << ' ' << *i; //printing results
 
cout << '\n';
return 0;
}


Output

 50 40 30 20 10

Time Complexity – constant O(1)

Auxiliary Space: O(1)

Applications crend : Returns a reverse iterator to the element following the last element of the reversed container. It corresponds to the element preceding the first element of the non-reversed container. This element acts as a placeholder, attempting to access it results in undefined behavior 

crbegin : Returns a reverse iterator to the first element of the reversed container. It corresponds to the last element of the non-reversed container. 

Both are used in deque operations, concurrently accessing or modifying different elements is safe. The copy construction or assignment of the returned iterator is also guaranteed to never throw exception.

Let us see the differences in a tabular form -:

  vector::crend() vector::crbegin()
1. It is used to return a const_reverse_iterator pointing to the theoretical element preceding the first element in the container. It is used to return  const_reverse_iterator to reverse beginning
2.

Its syntax is -:

const_reverse_iterator crend();

Its syntax is -:

const_reverse_iterator crbegin();

3. It does not take any parameters. It does not take any parameters.
4. Its complexity does not changes. Its complexity does not change.
5. Its iterator validity does not changes. Its iterator validity does not change.


Previous Article
Next Article

Similar Reads

array::crbegin() and array::crend() in C++ STL
array::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the last element in the container. Syntax:array_name.crbegin()Parameters: The function does not accepts any parameter. Return value: The function returns a reverse iterator pointing to the last element in the container. Program to demonstrate th
2 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
set crbegin() and crend() function in C++ STL
The set::crbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator set_name.crbegin() Parameters: The funct
2 min read
list crbegin() and crend() function in C++ STL
The list::crbegin() is a built-in function in c++ STL that returns a constant reverse iterator which points to the last element of the list i.e reversed beginning of container. The elements cannot be modified or changed as the iterator is a constant one. Syntax: list_name.crbegin()Parameters: The function does not accept any parameters. Return valu
3 min read
multiset crbegin() and crend() function in C++ STL
The multiset::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the multiset container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_reverse_iterator multiset_name.
2 min read
std::string::crbegin() and std::string::crend() in C++ with Examples
std::string::crbegin() The std::string::crbegin() is a string class built-in function that returns a constant reverse iterator referring to the last element in the string. Using this iterator starts the string traversal from the end of the string. Header File: #include &lt;string&gt; Template Class: template &lt;class C&gt; auto crbegin( const C
3 min read
deque crend in C++ STL
The deque::crend() is an inbuilt function in C++ STL which returns a constant reverse iterator which points to the position before the first element of the deque. Syntax deque_name.crend() Parameters: This function does not accept any parameters. Return Type: This function returns constant reverse iterator of deque. Example-1: C/C++ Code // C++ pro
2 min read
deque crbegin in C++ STL
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: deque
2 min read
vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
Article Tags :
Practice Tags :