C++ std::string push_back() Function

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

Definition

In C++, std::string::push_back() adds one character at the end of the string.

It is the character version of append. 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

Pass the character to append.

C++
s.push_back('!');
CallWhat it does
s.push_back(ch)Append character ch at the end.

s.push_back('x') is similar to s += 'x' or s.append(1, 'x').

Library declarations
C++
void push_back(char ch);

Parameters

ParameterDescription
chCharacter to append.

Return Value

Return ValueDescription
voidNo return value.

C++ Examples

Example 1 — Basic push_back()

Add an exclamation mark.

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

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

How It Works

  1. The string starts as "Hi".
  2. push_back('!') adds one character.
  3. Result is "Hi!".

Example 2 — Push Digits

Build a digit string in a loop.

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

int main() {
    std::string s;
    for (char d = '1'; d <= '3'; ++d) {
        s.push_back(d);
    }
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. The loop walks characters '1' to '3'.
  2. Each digit is pushed onto the string.
  3. Final text is "123".

Example 3 — Size After Push

Watch size increase by one.

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

int main() {
    std::string s = "ab";
    std::cout << s.size() << '\n';
    s.push_back('c');
    std::cout << s.size() << '\n';
    return 0;
}

How It Works

  1. Initial size is 2.
  2. One push_back adds one character.
  3. Size becomes 3.

Frequently Asked Questions

It appends one character at the end of the string.
No. push_back takes a single char. Use append or += for strings.
Only when size would exceed capacity.
Same effect for a single character.
Use pop_back() on a non-empty string.

Next: rbegin()

Keep going through the string functions. Reverse iterator to the last character.

rbegin() 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.

14 people found this page helpful