std::string::crbegin() and std::string::crend() in C++

In C++, std::string::crbegin() and std::string::crend() are functions that are members of the std::string class, which was added in C++11. They provide access to a string's reversed iterators, allowing users to iterate by traversing the string's elements in the opposite direction. In this article, we will discuss std::string::crbegin() and std::string::crend() functions with their syntax and examples.

What is std::string::crbegin()?

The std::string::crbegin() method is a string class built-in function that returns an integer reverse iterator referencing the string's final element. This iterator begins the string's traversal at the end of the specified string.

Header File:

Function: std::string::crbegin() returns a constant reversed iterator pointed to the string's last character.

Use: It is frequently used in a range-based while loop or in any case in which you wish to run through the string in the opposite order.

Syntax:

It has the following syntax:

Parameters: This function requires no parameters.

Return Value: The function std::string::crbegin() produces a constant reverse iterator referencing the last element in the string. The following program is an example of string::crbegin():

Example 1:

Output:

gnimmargorP

What is std::string::crend()

The std::string::crend() method is a string class built-in function that returns a constant reversed iterator referring to the imaginary element before the first element within the string. This iteration is used to return to the beginning of the string when traversing the string in the opposite order.

Template Class:

Parameters: This function does not require any arguments.

Return value: This function's return value is std::string::crend(), which delivers a constant reverse iteration pointing to the component in the string before the initial element. The following implementation implements string::crend():

Example:

Output:

The last index is 10





Latest Courses