C++ std::string back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::back() returns a reference to the last character of the string.

The string must not be empty. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call with empty brackets on a non-empty string.

C++
char c = s.back();
s.back() = '!';
CallWhat it does
s.back()Reference to the last character.

Often paired with push_back / pop_back. After pop_back, do not use an old reference from back().

Library declarations
C++
char& back();
const char& back() const;

Parameters

ParameterDescription
Noneback() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
char&Reference to the last character.

C++ Examples

Example 1 — Basic back()

Print the last character.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Code";
    std::cout << s.back() << '\n';
    return 0;
}

How It Works

  1. "Code" ends with 'e'.
  2. back() returns that character.
  3. The rest of the string is unchanged.

Example 2 — Assign back()

Replace the final character.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "fun.";
    s.back() = '!';
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. The last character was '.'.
  2. Assignment through back() replaces it with '!'.
  3. The printed string shows the update.

Example 3 — back() Then pop_back()

Read the last char, then remove it.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "abX";
    std::cout << "last: " << s.back() << '\n';
    s.pop_back();
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. back() reads 'X' without removing it.
  2. pop_back() then deletes the last character.
  3. The string left is "ab".

Frequently Asked Questions

It returns a reference to the last character of a non-empty string.
No. It is undefined behavior. Check empty() first.
back() reads or changes the last character. pop_back() removes it.
For a non-empty string, yes they refer to the same character. at throws if size is 0; back does not throw.
You can, but the pointer becomes invalid if the string reallocates or the character is removed.

Next: begin()

Keep going through the string functions. Iterator to the first character (or end() if empty).

begin() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful