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('!');
Call
What 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
Parameter
Description
ch
Character to append.
Return Value
Return Value
Description
void
No 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;
}
Output
Hi!
How It Works
The string starts as "Hi".
push_back('!') adds one character.
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;
}