C++ Reference ยท Strings and I/O
C++ string::append and operator+=
How to add text to a std::string with append, push_back, operator+= and operator+: all overloads, append_range, resize_and_overwrite and performance tips.
append adds characters to the end of a std::string and returns a reference to the string, so calls can be chained. operator+= is the short spelling for the common overloads (another string, a C string, one character, an initializer list, a string-view-like). push_back adds exactly one character.
Appending is amortized constant time per character, because the string grows its capacity geometrically. operator+ is different: it builds a brand new string, so chains of a + b + c + d create temporaries. For repeated additions, append to one string instead.
std::string s = "abc"; s += "def"; // "abcdef" s.append(3, '!'); // "abcdef!!!" s.push_back('?'); // "abcdef!!!?"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
basic_string& append(const basic_string& str); basic_string& append(const basic_string& str, size_type pos, size_type count = npos); basic_string& append(const char* s, size_type count); basic_string& append(const char* s); basic_string& append(size_type count, char ch); template<class It> basic_string& append(It first, It last); basic_string& append(std::initializer_list<char> init); basic_string& append(const T& t); // string_view-like, C++17 template<class R> basic_string& append_range(R&& rg); // C++23 basic_string& operator+=(const basic_string& str); basic_string& operator+=(const char* s); basic_string& operator+=(char ch); void push_back(char ch);
| Name | Description |
|---|---|
str, pos, count |
A source string and a slice of it; pos > str.size() throws std::out_of_range. |
s |
A pointer to characters; without count it must be null-terminated. |
ch |
A character; with count it is repeated. |
first, last |
An iterator range. |
rg |
A range whose elements convert to char. |
Returns: *this for append and operator+=; nothing for push_back.
Errors: std::length_error when the result would exceed max_size(). Appending may reallocate, which invalidates iterators, pointers and references into the string.
Usage notes
+=versus+.a += bmodifiesain place.a + breturns a new string; at least one operand must be astd::string, so"a" + "b"does not compile (and"a" + 1is pointer arithmetic).- Adding a number with
+=appends a character with that code:s += 65;appends'A'. Usestd::to_string(see stoi, stod and to_string) orstd::formatfor digits. - Rvalue-friendly
+. Since C++11,std::string{...} + xreuses the left temporary's buffer, soa + b + callocates less than it looks like; still,+=in a loop is best. - Reserve when you know the final size:
s.reserve(n)avoids intermediate reallocations. - Self-append (
s += s,s.append(s, 0, 2)) is well defined. - Embedded nulls are copied only by the overloads with an explicit length or by
std::string/string_viewarguments;append("a\0b")stops at the null. - Output formatting often replaces long append chains: see std::format.