This function returns a direct reference to the first character of the string. This shall only be used on non-empty strings.
This can be used to access the first character of the string as well as to insert a character at the start of the string. Length of the string remains same after inserting a character, first character gets replaced by the new one.
string str ("GeeksforGeeks"); Accessing first character char first_char = str.front(); Inserting character at start of string str.front() = '#';
Parameter: This function takes no parameter
Return value: A reference to the first character in the string
Exception: If the string is empty it shows undefined behavior.
Below examples illustrate the use of the above method:
First character of string = G New string = #eeksforGeeks
Program 2: It shows undefined behavior when the string is empty.
First character of string = New string =
- cauchy_distribution a() in C++ with Examples
- ratio_equal() in C++ with examples
- SDL library in C/C++ with examples
- wmemset() in C/C++ with Examples
- std::allocator() in C++ with Examples
- ratio_not_equal() in C++ with examples
- iswgraph() in C/C++ with Examples
- iswprint() in C/C++ with Examples
- negative_binomial_distribution in C++ with Examples
- memset() in C with examples
- mbrtoc16() in C/C++ with Examples
- Pointers in C/C++ with Examples
- std::mismatch() with examples in C++
- feclearexcept in C++ with Examples
- ios bad() function 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 firstname.lastname@example.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.