public member function

std::string::append

<string>
string& append ( const string& str );
string& append ( const string& str, size_t pos, size_t n );
string& append ( const char* s, size_t n );
string& append ( const char* s );
string& append ( size_t n, char c );
template <class InputIterator>
   string& append ( InputIterator first, InputIterator last );
Append to string
The current string content is extended by adding an additional appending string at its end.

The arguments passed to the function determine this appending string:

string& append ( const string& str );
Appends a copy of str.
string& append ( const string& str, size_t pos, size_t n );
Appends a copy of a substring of str. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of string is reached before).
If the position passed is past the end of str, an out_of_range exception is thrown.
string& append ( const char * s, size_t n );
Appends a copy of the string formed by the first n characters in the array of characters pointed by s.
string& append ( const char * s );
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 the first ocurrence of a null character (as determined by traits.length(s)).
string& append ( size_t n, char c );
Appends a string formed by the repetition n times of character c.
template<class InputIterator> string& append (InputIterator first, InputIterator last);
If InputIterator is an integral type, behaves as the previous member function version, effectively appending a string formed by the repetition first times of the character equivalent to last.
In any other case, the parameters are considered iterators and the content is appended by the elements that go from element referred by iterator first to the element right before the one referred by iterator last.

Parameters

str
Another object of class string whose content is entirely or partially appended to the content of this.
pos
Starting position of the substring in str that is appended. Notice that the first position has a value of 0, not 1.
n
Number of characters to append.
s
Array with a sequence of characters.
In the third member version, the length is not determined by any occurrence of null-characters, but by parameter n.
By contrast, in the fourth member version, this is a null-terminated character sequence whose length is determined by the first occurrence of a null-character.
c
Character value, repeated n times.
start
If along with last, both are integers, it is equivalent to parameter n, otherwise it is an iterator referring to the beginning of a sequence of characters.
last
If along with start, both are integers, it is equivalent to parameter c, otherwise it is an iterator referring to the past-the-end element of a sequence of characters.

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
23
// appending to string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  string str2="Writing ";
  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(10,'.');                     // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<int>(5,0x2E);                // "....."

  cout << str << endl;
  return 0;
}


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

Basic template member declarations

( basic_string<charT,traits,Allocator> )
1
2
3
4
5
6
7
8
9
typedef typename Allocator::size_type size_type;
basic_string& append ( const basic_string& str );
basic_string& append ( const basic_string& str, size_type pos,
                       size_type n );
basic_string& append ( const charT* s, size_type n );
basic_string& append ( const charT* s );
basic_string& append ( size_type n, charT c );
template <class InputIterator>
  basic_string& append ( InputIterator first, InputIterator last );


See also