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 <string>
Template Class:
template <class C>
auto crbegin( const C& c )
-> decltype(std::rbegin(c));
Syntax:
string_name.crbegin()
Parameters: This function doesn’t required any parameters.
Return Value: This function std::string::crbegin() returns a constant reverse iterator referring to the last element in the string.
Below is the program to illustrate string::crbegin():
Program 1:
// C++ program to illustrate // std::string:crbegin() #include <iostream> #include <string> using namespace std; // Driver Code int main() { // Given string string str("GeeksForGeeks"); // Traverse the given string using // reverse iterator crbegin() for (auto it = str.crbegin(); it != str.crend(); it++) { // Print the elements cout << *it; } return 0; } |
skeeGroFskeeG
std::string::crend()
The std::string::crend() is a string class built-in function that returns a constant reverse iterator pointing to the theoretical element before the first element in the string. This iterator is used to reach the starting of the string while traversing string in reverse order.
Template Class:
template <class C>
auto crend( const C& c )
-> decltype(std::rend(c));
Syntax:
string_name.crend()
Parameters: This function doesn’t required any parameters.
Return Value: This function std::string::crend() returns a constant reverse iterator pointing to the element before the first element in the string.
Below is the program to illustrate string::crend():
Program 2:
// C++ program to illustrate // std::string:crend() #include <iostream> #include <string> using namespace std; // Driver Code int main() { // Given string string str("GeeksForGeeks"); // Find string length int N = str.length(); // Given character char ch = 'k'; // To check whether the char is // present or not bool a = true; // Traverse the given string using // reverse iterator crbegin() and // check if ch is present or not for (auto it = str.crbegin(); it != str.crend(); it++) { if (*it == ch) { cout << "The last index is " << N - (it - str.crbegin() + 1) << endl; a = false; break; } } if (a) { cout << "Character is not present"; } return 0; } |
The last index is 11
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- cin get() in C++ with Examples
- Pi(π) in C++ with Examples
- std::less in C++ with Examples
- std::is_trivially_move_assignable in C++ with Examples
- std::add_lvalue_reference in C++ with Examples
- std::is_nothrow_constructible in C++ with Examples
- std::is_trivially_move_constructible in C++ with Examples
- std::rank in C++ with Examples
- std::is_nothrow_copy_constructible in C++ with Examples
- std::make_signed in C++ with Examples
- std::greater in C++ with Examples
- std::is_nothrow_assignable in C++ with Examples
- cauchy_distribution a() in C++ with Examples
- ratio_equal() in C++ with examples
- std::to_address in C++ with Examples
- std::is_trivially_assignable in C++ with Examples
- std::remove_const in C++ with Examples
- std::is_nothrow_destructible in C++ with Examples
- std::remove_volatile in C++ with Examples
- std::equal_to in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

