On this page
std::basic_string<CharT,Traits,Allocator>::append
| (1) | ||
|
(until C++20) | |
|
(since C++20) | |
| (2) | ||
|
(until C++20) | |
|
(since C++20) | |
| (3) | ||
|
(until C++14) | |
|
(since C++14) (until C++20) |
|
|
(since C++20) | |
| (4) | ||
|
(until C++20) | |
|
(since C++20) | |
| (5) | ||
|
(until C++20) | |
|
(since C++20) | |
| (6) | ||
|
(until C++20) | |
|
(since C++20) | |
| (7) | ||
|
(since C++11) (until C++20) |
|
|
(since C++20) | |
| (8) | ||
|
(since C++17) (until C++20) |
|
|
(since C++20) | |
| (9) | ||
|
(since C++17) (until C++20) |
|
|
(since C++20) |
Appends additional characters to the string.
count copies of character ch.
str.
[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_rangeis thrown.
[s, s + count). This range can contain null characters.
s, as if by append(s, Traits::length(s)).
[first, last).
This overload has the same effect as overload (1) if |
(until C++11) |
This overload only participates in overload resolution if |
(since C++11) |
ilist.
t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then appends all characters from sv as if by append(sv.data(), sv.size()).
std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.
t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then appends the characters from the subview [pos, pos + count) of sv.
- If the requested subview extends past the end of
sv, or ifcount == npos, the appended subview is[pos,sv.size()). - If
pos >= sv.size(),std::out_of_rangeis thrown.
std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.
Parameters
| count | - | number of characters to append |
| pos | - | the index of the first character 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 |
| t | - | object convertible to std::basic_string_view 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 result in size() > max_size(), throws std::length_error.
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).
Example
#include <iostream>
#include <string>
int main()
{
std::basic_string<char> str = "string";
const char* cptr = "C-string";
const char carr[] = "Two and one";
std::string output;
// 1) Append a char 3 times.
// Notice, this is the only overload accepting chars.
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) ***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
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 guarantee | added strong exception safety guarantee |
| LWG 2946 | C++17 | overload (8) causes ambiguity in some cases | avoided by making it a template |
See also
|
(C++23)
|
appends a range of characters to the end (public member function) |
| appends characters to the end (public member function) |
|
| concatenates two strings (function) |
|
| concatenates a certain amount of characters of two strings (function) |
|
| appends a copy of one wide string to another (function) |
|
| appends a certain amount of wide characters from one wide string to another (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/string/basic_string/append