public member function
<string>

std::basic_string::append

string (1)
basic_string& append (const basic_string& str);
substring (2)
basic_string& append (const basic_string& str, size_type subpos, size_type sublen);
c-string (3)
basic_string& append (const charT* s);
buffer (4)
basic_string& append (const charT* s, size_type n);
fill (5)
basic_string& append (size_type n, charT c);
range (6)
template <class InputIterator>   basic_string& append (InputIterator first, InputIterator last);
string (1)
basic_string& append (const basic_string& str);
substring (2)
basic_string& append (const basic_string& str, size_type subpos, size_type sublen);
c-string (3)
basic_string& append (const charT* s);
buffer (4)
basic_string& append (const charT* s, size_type n);
fill (5)
basic_string& append (size_type n, charT c);
range (6)
template <class InputIterator>   basic_string& append (InputIterator first, InputIterator last);
initializer list(7)
basic_string& append (initializer_list<charT> il);
string (1)
basic_string& append (const basic_string& str);
substring (2)
basic_string& append (const basic_string& str, size_type subpos, size_type sublen = npos);
c-string (3)
basic_string& append (const charT* s);
buffer (4)
basic_string& append (const charT* s, size_type n);
fill (5)
basic_string& append (size_type n, charT c);
range (6)
template <class InputIterator>   basic_string& append (InputIterator first, InputIterator last);
initializer list(7)
basic_string& append (initializer_list<charT> il);
Append to string
Extends the basic_string by appending additional characters at the end of its current value:

(1) string
Appends a copy of str.
(2) substring
Appends a copy of a substring of str. The substring is the portion of str that begins at the character position subpos and spans sublen characters (or until the end of str, if either str is too short or if sublen is basic_string::npos).
(3) c-string
Appends a copy of the string formed by the null-terminated character sequence (C-string) pointed by s.
The length of this character sequence is determined by calling traits_type::length(s).
(4) buffer
Appends a copy of the first n characters in the array of characters pointed by s.
(5) fill
Appends n consecutive copies of character c.
(6) range
Appends a copy of the sequence of characters in the range [first,last), in the same order.
(7) initializer list
Appends a copy of each of the characters in il, in the same order.

Parameters

str
Another basic_string object of the same type (with the same class template arguments charT, traits and Alloc), whose value is appended.
subpos
Position of the first character in str that is copied to the object as a substring.
If this is greater than str's length, it throws out_of_range.
Note: The first character in str is denoted by a value of 0 (not 1).
sublen
Length of the substring to be copied (if the string is shorter, as many characters as possible are copied).
A value of basic_string::npos indicates all characters until the end of str.
s
Pointer to an array of characters (such as a c-string).
n
Number of characters to copy.
c
Character value, repeated n times.
first, last
Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the characters between first and last, including the character pointed by first but not the character pointed by last.
The function template argument InputIterator shall be an input iterator type that points to elements of a type convertible to charT.
If InputIterator is an integral type, the arguments are casted to the proper types so that signature (5) is used instead.
il
An initializer_list object.
These objects are automatically constructed from initializer list declarators.

charT is basic_string's character type (i.e., its first template parameter).
Member type size_type is an unsigned integral type.

Return Value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// appending to string
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string str2="Writing ";
  std::string str3="print 10 and then 5 more";

  // used in the same order as described above:
  str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10u,'.');                    // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<int>(5,0x2E);                // "....."

  std::cout << str << '\n';
  return 0;
}

Output:
Writing 10 dots here: .......... and then 5 more.....


Complexity

Unspecified, but generally up to linear in the new string length.

Iterator validity

Any iterators, pointers and references related to this object may be invalidated.

Data races

The object is modified.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the basic_string.

If s does not point to an array long enough, or if the range specified by [first,last) is not valid, it causes undefined behavior.

If subpos is greater than str's length, an out_of_range exception is thrown.
If the resulting string length would exceed the max_size, a length_error exception is thrown.
If the type uses the default allocator, a bad_alloc exception is thrown if the function needs to allocate storage and fails.

See also