public member function
<string>

std::basic_string::front

      charT& front();const charT& front() const;
Access first character
Returns a reference to the first character of the basic_string.

Unlike member basic_string::begin, which returns an iterator to this same character, this function returns a direct reference.

This function shall not be called on empty strings.

Parameters

none

Return value

A reference to the first character in the basic_string.

If the basic_string object is const-qualified, the function returns a const charT&. Otherwise, it returns a charT&.

charT is basic_string's character type (i.e., its first template parameter).

Example

1
2
3
4
5
6
7
8
9
10
11
// string::front
#include <iostream>
#include <string>

int main ()
{
  std::string str ("test string");
  str.front() = 'T';
  std::cout << str << '\n';
  return 0;
}

Output:
Test string


Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed (neither the const nor the non-const versions modify the container).
The reference returned can be used to access or modify characters. Concurrently accessing or modifying different characters is safe.

Exception safety

If the basic_string is not empty, the function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.

See also