Stdstringcrbegin And Stdstringcrend In C++ - C++ Programming Tutorial
C++ Course / Strings / Stdstringcrbegin And Stdstringcrend In C++

Stdstringcrbegin And Stdstringcrend In C++

BLUF: Mastering Stdstringcrbegin And Stdstringcrend In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Stdstringcrbegin And Stdstringcrend In C++

C++ is renowned for its efficiency. Learn how Stdstringcrbegin And Stdstringcrend In C++ enables low-level control and high-performance computing in the tutorial below.

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:

Example

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

Example

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:

Example

// 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:

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:

Example

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:

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:

Output

The last index is 10

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience