std::string::back() in C++ with Examples Last Updated : 28 May, 2019 Comments Improve Suggest changes Like Article Like Report This function returns a direct reference to the last character of the string. This shall only be used on non-empty strings. This can be used to access the last character of the string as well as to append a character at the end of the string. Length of the string remains unchanged after appending a character, last character of string gets replaced by the new one Syntax string str ("GeeksforGeeks"); Accessing last character char end_char = str.back(); Appending character at end of string str.back() = '#'; Parameter: This function takes no parameter Return value: A reference to the last character in the string Exception: If the string is empty it shows undefined behavior. Below examples illustrate the use of the above method: Program 1: CPP // C++ program to demonstrate // the use of the above method #include <iostream> // for std::string::back #include <string> using namespace std; int main() { string str("GeeksforGeeks"); // Accessing last character of string char end_char = str.back(); cout << "Last character of string = " << end_char << endl; // Appending a character to // the end of string str.back() = '#'; cout << "New string = " << str << endl; return 0; } Output: Last character of string = s New string = GeeksforGeek# Program 2: It shows undefined behavior when the string is empty. CPP // C++ program to demonstrate // the use of the above method #include <iostream> // for std::string::front #include <string> using namespace std; int main() { string str(""); // Empty string // trying to access last character // of an empty string char end_char = str.back(); cout << "Last character of string = " << end_char << endl; // Appending a character to // the end of an empty string str.back() = '#'; cout << "New string = " << str << endl; return 0; } Output: Last character of string = New string = ÉGà @ ·ùþ?aPé£Îý @ ?k¯Ã ÿÿÿÿÿÿÿXé£Îý ÿÿÿ @ Ï?¨Õ,Ä @ Pé£Îý Ï??Á±?ðÏ?Ø%Bð 3ÇGà ¸ÂEà :µ# @ Pé£Îý I@ Hé£Îý ªÿ£Îý ! P·Îý ÿû? d @ @ 8 ÀFà @ é é é é ©ê£Îý Ñÿ£Îý ¹ê£Îý ¾·ùþ?aDdCâ?gCx86_64 Reference: std::string::back() Comment More infoAdvertise with us Next Article std::string::back() in C++ with Examples S sanjeev2552 Follow Improve Article Tags : C++ cpp-string CPP-Functions Practice Tags : CPP Similar Reads std::string::front() in C++with Examples It is used to access the first character from the string. It returns a reference to the first character of the string. Unlike member string::begin, which returns an iterator to this same character, this function returns a direct reference. Syntax: string str ("GeeksforGeeks"); Accessing first charac 2 min read basic_istream::seekg() in C++ with Examples The basic_stream::seekg() method is used to set position of the next character to be extracted from the input stream. This function is present in the iostream header file. Below is the syntax for the same: Header File: #include<iostream> Syntax: basic_istream& seekg (pos_type pos); Paramet 2 min read basic_istream::putback() in C++ with Examples The basic_istream::putback() used to put the character back in the input string. This function is present in the iostream header file. Below is the syntax for the same: Header File: #include<iostream> Syntax: basic_istream& putback (char_type ch); Parameter: ch: It represents the character to 2 min read std::basic_istream::getline in C++ with Examples The std::basic_istream::getline is used to extract the characters from stream until end of line or the extracted character is the delimiting character. The delimiting character is the new line character i.e '\n'. This function will also stop extracting characters if the end-of-file is reached if inp 2 min read std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar 8 min read Like