What is string::crend() in C/C++?

The string::crend() function returns the iterator that points to the first character in the string. It is used to traverse the string in reverse order.

Figure 1 shows the visual representation of the crend() function.

Figure 1: Visual representation of crend() function

Header file

This function requires a header file:

#include <string>

Syntax

string.crend()

Return value

crend() returns the iterator pointing to the first character in the string.

Example

The example below shows how we can use crend() to iterate the string in reverse order.

#include <iostream>
using namespace std;
//Header File
#include <string>
int main(){
//string declared
string STR="EDPRESSO";
cout<<"The reverse order of string "<<STR<<": ";
//iterator is being checked against crend() (first character) inorder to iterate the string in reverse
for (auto iterator=STR.crbegin(); iterator !=STR.crend(); iterator++ )
cout<<*iterator;
}

Free Resources