Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
c++
string
communitycreator

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

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The string::crbegin() function returns the iterator pointing to the last character in a string. This is used to traverse the string in reverse order. Figure 1 shows the visual representation of the crbegin() function.

Figure 1: Visual representation of crbegin() function

Header file

This function requires a header file:

#include <string>

Syntax

string.crbegin()

Return Value

crbegin() returns the iterator that points to the last character in the string.

Example

The example below shows how we can use crbegin() 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 created using crbegin() inorder to iterate the string in reverse
for (auto iterator=STR.crbegin(); iterator !=STR.crend(); iterator++ )
cout<<*iterator;
}

RELATED TAGS

c
c++
string
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring