Scroll to navigation

std::basic_string::append(3) C++ Standard Libary std::basic_string::append(3)

NAME

std::basic_string::append - std::basic_string::append

Synopsis

basic_string& append( size_type count, CharT ch ); (1) (constexpr since
C++20) basic_string& append( const CharT* s, size_type (2) (constexpr since count ); C++20) basic_string& append( const CharT* s ); (3) (constexpr since
C++20) template< class SV > (since C++17) basic_string& append( const SV& t ); (4) (constexpr since
C++20) template< class SV >
(since C++17) basic_string& append( const SV& t, size_type pos, (5) (constexpr since
C++20) size_type count = npos ); basic_string& append( const basic_string& str ); (6) (constexpr since
C++20) basic_string& append( const basic_string& str, (until C++14) size_type pos, size_type count ); basic_string& append( const basic_string& str, (since C++14) size_type pos, size_type count = npos ); (constexpr since
(7) C++20) template< class InputIt > (8) (constexpr since basic_string& append( InputIt first, InputIt last ); C++20) basic_string& append( std::initializer_list<CharT> (since C++11) ilist ); (9) (constexpr since
C++20)

Appends additional characters to the string.

1) Appends count copies of character ch. 2) Appends characters in the range [s, s + count). If [s, s + count) is not a valid range, the behavior is undefined. 3) Equivalent to return append(s, Traits::length(s));. 4,5) Appends characters in a string view sv constructed from t.
* If only t is provided, all characters in sv are appended.
* If pos is also provided:
* If count is npos, all characters in sv starting from pos are appended.
* Otherwise, the std::min(count, sv.size() - pos) characters in sv starting from
pos are appended. These overloads participate in overload resolution only if all following conditions are satisfied:
* std::is_convertible_v<const SV&, std::basic_string_view<CharT, Traits>> is true.
* std::is_convertible_v<const SV&, const CharT*> is false. 4) Equivalent to std::basic_string_view<CharT, Traits> sv = t; return append(sv.data(), sv.size());. 5) Equivalent to std::basic_string_view<CharT, Traits> sv = t; return append(sv.substr(pos, count));. 6,7) Appends characters in another string str.
* If only str is provided, all characters in it are appended.
* If pos is also provided:
* If count is npos, all characters in str starting from pos are appended.
* Otherwise, the std::min(count, str.size() - pos) characters in str starting from
pos are appended. 6) Equivalent to return append(str.data(), str.size());.

7) Equivalent to return append(std::basic_string_view<CharT, Traits> (since C++20) (str).substr(pos, count));.

8) Equivalent to return append(basic_string(first, last, get_allocator()));.

This overload has the same effect as overload (1) if InputIt is an integral (until C++11) type. This overload participates in overload resolution only if InputIt satisfies (since C++11) the requirements of LegacyInputIterator.

9) Equivalent to return append(ilist.begin(), ilist.size());.

Parameters

count - number of characters to append ch - character value to append s - pointer to the character string to append t - object convertible to std::basic_string_view with the characters to append pos - the index of the first character to append str - string to append first, last - range of characters to append ilist - initializer list with the characters to append

Return value

*this

Complexity

There are no standard complexity guarantees, typical implementations behave similar to std::vector::insert().

Exceptions

If the operation would cause size() to exceed max_size(), throws std::length_error.

5) If pos > sv.size() is true, throws std::out_of_range. 7) If pos > str.size() is true, throws std::out_of_range.

If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

Example

// Run this code

#include <cassert> #include <string>

int main() {
std::string str = "std::string";
const char* cptr = "C-string";
const char carr[] = "range";


std::string result;


// 1) Append a char 3 times.
// Note: This is the only overload accepting “CharT”s.
result.append(3, '*');
assert(result == "***");


// 2) Append a fixed-length C-string
result.append(cptr, 5);
assert(result == "***C-str");


// 3) Append a null-terminated C-string
// Note: Because “append” returns *this, we can chain calls together.
result.append(1, ' ').append(cptr);
assert(result == "***C-str C-string");


// 6) Append a whole string
result.append(1, ' ').append(str);
assert(result == "***C-str C-string std::string");


// 7) Append part of a string
result.append(str, 3, 2);
assert(result == "***C-str C-string std::string::");


// 8) Append range
result.append(&carr[2], &carr[3]);
assert(result == "***C-str C-string std::string::n");


// 9) Append initializer list
result.append({'p', 'o', 's'});
assert(result == "***C-str C-string std::string::npos"); }


Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.


DR Applied to Behavior as published Correct behavior LWG 847 C++98 there was no exception safety added strong exception safety
guarantee guarantee
the behavior of overload (7) was always throws an exception in this LWG 2250 C++98 undefined if pos > str.size() is case
true
overload (8) used a default LWG 2788 C++98 constructed obtains the allocator
allocator to construct the from get_allocator()
temporary string LWG 2946 C++17 overload (4) causes ambiguity in avoided by making it a template
some cases

See also

append_range appends a range of characters to the end (C++23) (public member function) operator+= appends characters to the end
(public member function) strcat concatenates two strings
(function) strncat concatenates a certain amount of characters of two strings
(function) wcscat appends a copy of one wide string to another
(function) wcsncat appends a certain amount of wide characters from one wide string to another
(function)

2026.11.10 http://cppreference.com