In C++, the methods std::string::crbegin and std::string::crend belong to the std::string class and were introduced in C++11. These functions grant users the ability to work with reversed iterators of a string, enabling iteration through the string's elements in a reversed order. This tutorial will delve into the syntax and usage examples of std::string::crbegin and std::string::crend.
What is std::string::crbegin?
The std::string::crbegin function is a member function of the string class that provides an iterator in reverse order, pointing to the last element of the string. It allows traversal of the string starting from the end.
Header File:
#include<string>
The method std::string::crbegin provides a constant reverse iterator that points to the final character of the string.
It is commonly utilized in a while loop based on a range or in any scenario where you need to iterate through the string in reverse order.
Syntax:
It has the following syntax:
string_name.crbegin()
Parameters: This function requires no parameters.
Return Output: The std::string::crbegin function generates a constant reverse iterator that points to the final element within the string. Presented below is a sample showcasing the utilization of string::crbegin:
Example 1:
// C++ Program to use the std::string:crbegin() method
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
// Given string
string strs("Programming");
//// reverse iterator crbegin() traverses the given string
for (auto ite = strs.crbegin();
ite != strs.crend(); ite++) {
//display of elements
cout << *ite;
}
return 0;
}
Output:
gnimmargorP
What is std::string::crend
The std::string::crend function is a predefined function in the string class which provides a constant reverse iterator pointing to the theoretical element before the initial element in the string. This iterator is beneficial for looping back to the start of the string while navigating through it in the reverse direction.
Template Class:
template <class C>
auto crend( const C& ch )
-> decltype(std::rend(ch));
Parameters: This function does not necessitate any parameters to be passed.
Return Output: The outcome of this function is std::string::crend, providing a constant reverse iterator pointing to the element in the string preceding the first element. Below is the code snippet showcasing the implementation of string::crend:
Example:
//C++ Program for implementation of
// std::string:crend()
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
// Given string
string strs("Programming");
// Find string length
int Num = strs.length();
// Given character
char cha = 'g';
// variable to check
bool a1 = true;
// Traverse the provided string using the reverse iterator crbegin() and determine whether or not cha is present.
for (auto ite = strs.crbegin();
ite != strs.crend(); ite++) {
if (*ite == cha) {
cout << "The last index is "
<< Num - (ite - strs.crbegin() + 1)
<< endl;
a1 = false;
break;
}
}
if (a1) {
cout << "The given character is not present";
}
return 0;
}
Output:
The last index is 10