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
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:
Last character of string = s New string = GeeksforGeek#
Program 2: It shows undefined behavior when the string is empty.
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
- ratio_equal() in C++ with examples
- set_symmetric_difference in C++ with Examples
- ratio_not_equal() in C++ with examples
- std::allocator() in C++ with Examples
- SDL library in C/C++ with examples
- Manipulators in C++ with Examples
- cauchy_distribution a() in C++ with Examples
- std::mismatch() with examples in C++
- memset() in C with examples
- C/C++ For loop with Examples
- iswgraph() in C/C++ with Examples
- iswprint() in C/C++ with Examples
- negative_binomial_distribution in C++ with Examples
- mbrtoc32() in C/C++ with Examples
- mbrtoc16() in C/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 email@example.com. 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.