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 =
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- std::less in C++ with Examples
- cin get() in C++ with Examples
- Pi(π) in C++ with Examples
- ratio_equal() in C++ with examples
- ios bad() function in C++ with Examples
- C/C++ if else if ladder with Examples
- C/C++ do while loop with Examples
- C/C++ if statement with Examples
- C/C++ if else statement with Examples
- std::to_address in C++ with Examples
- ios eof() function in C++ with Examples
- C/C++ while loop with Examples
- std::is_heap( ) in C++ with Examples
- ratio_not_equal() in C++ with examples
- std::equal_to in C++ with Examples