| (1) | |||
| (2) | |||
(3) | ||||
| (until C++14) | |||
| (since C++14) | |||
| (4) | |||
| (5) | |||
| (6) | |||
| (7) | (since C++11) |
Appends additional characters to the string.
1) Appends count
copies of character ch
.
2) Appends string str
.
3) Appends a substring [pos, pos+count)
of str
. If the requested substring lasts past the end of the string, or if count == npos
, the appended substring is [pos, size())
. If pos >= str.size()
, std::out_of_range
is thrown.
4) Appends the first count
characters of character string pointed to by s
. s
can contain null characters.
5) Appends the null-terminated character string pointed to by s
. The length of the string is determined by the first null character.
6) Appends characters in the range [first, last)
.
This overload has the same effect as overload (1) if InputIt is an integral type. | (until C++11) |
This overload only participates in overload resolution if InputIt satisfies InputIterator . | (since C++11) |
7) Appends characters in the initializer list ilist
.
Parameters
count | - | number of characters to append |
ch | - | character value to append |
first, last | - | range of characters to append |
str | - | string to append |
s | - | pointer to the character string 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 an exception is thrown for any reason, this function has no effect (strong exception guarantee). (since C++11).
If the operation would result in size() > max_size()
, throws std::length_error
.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <string> #include <iostream> int main() { std::basic_string< char > str = "string" ; const char * cptr = "C-string" ; const char carr[] = "Two and one" ; std::string output; // 1) Repeat a char output.append(3, '*' ); std::cout << "1) " << output << "\n" ; // 2) Append a whole string output.append(str); std::cout << "2) " << output << "\n" ; // 3) Append part of a string (last 3 letters, in this case) output.append(str, 3, 3); std::cout << "3) " << output << "\n" ; // 4) Append part of a C-string // Notice, because `append` returns *this, we can chain calls together output.append(1, ' ' ).append(carr, 4); std::cout << "4) " << output << "\n" ; // 5) Append a whole C-string output.append(cptr); std::cout << "5) " << output << "\n" ; // 6) Append range output.append(&carr[3], std::end(carr)); std::cout << "6) " << output << "\n" ; // 7) Append initializer list output.append({ ' ' , 'l' , 'i' , 's' , 't' }); std::cout << "7) " << output << "\n" ; } |
Output:
1 2 3 4 5 6 7 | 1) *** 2) ***string 3) ***stringing 4) ***stringing Two 5) ***stringing Two C-string 6) ***stringing Two C-string and one 7) ***stringing Two C-string and one list |
See also
operator+=
| appends characters to the end (public member function) |
Please login to continue.