std::basic_string::append(3)
π NAME π
Okay so welcome to std::basic_string::append(3), bestie β this page comes from the C++ Standard Libary manual (yes, that spelling is staying, it's iconic π
). The name of this whole serve is std::basic_string::append β std::basic_string::append, and it's literally the member function for adding more characters to the end of a string. Periodt.
πΈ Synopsis πΈ
basic_string& append( size_type count, CharT ch ); (1) (constexpr since C++20)
basic_string& append( const basic_string& str ); (2) (constexpr since C++20)
basic_string& append( const basic_string& str,
size_type pos, size_type count ); (3) (until C++14)
basic_string& append( const basic_string& str,
size_type pos, size_type count = npos ); (3) (since C++14) (constexpr since C++20)
basic_string& append( const CharT* s, size_type count ); (4) (constexpr since C++20)
basic_string& append( const CharT* s ); (5) (constexpr since C++20)
template< class InputIt >
basic_string& append( InputIt first, InputIt last ); (6) (constexpr since C++20)
basic_string& append( std::initializer_list<CharT> ilist ); (7) (since C++11) (constexpr since C++20)
template< class StringViewLike >
basic_string& append( const StringViewLike& t ); (8) (since C++17) (constexpr since C++20)
template< class StringViewLike >
basic_string& append( const StringViewLike& t,
size_type pos, size_type count = npos ); (9) (since C++17) (constexpr since C++20)
Appends additional characters to the string. That's the whole vibe, but the overloads each have their own little personality, so let's get into it.
- Appends
countcopies of characterch. β¨ - Appends string
str. π - Appends a substring
[pos, pos + count)ofstr.- 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. Not cute.
- If the requested substring lasts past the end of the string, or if
- Appends characters in the range
[s, s + count). This range can contain null characters. If[s, s + count)is not a valid range, the behavior is undefined. That's a real warning, bestie β undefined behavior is not a slay. - Appends the null-terminated character string pointed to by
s, as if byappend(s, Traits::length(s)). - Appends characters in the range
[first, last).- Until C++11: this overload has the same effect as overload (1) if
InputItis an integral type. - Since C++11: this overload only participates in overload resolution if
InputItqualifies as aLegacyInputIterator.
- Until C++11: this overload has the same effect as overload (1) if
- Appends characters from the initializer list
ilist. - Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then appends all characters fromsvas if byappend(sv.data(), sv.size()). This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true andstd::is_convertible_v<const StringViewLike&, const CharT*>is false. - Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then appends the characters from the subview[pos, pos + count)ofsv.- 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. Again: rude, but that's the rule. This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true andstd::is_convertible_v<const StringViewLike&, const CharT*>is false.
- If the requested subview extends past the end of
π Parameters π
countβ number of characters to appendposβ the index of the first character to appendchβ character value to appendfirst, lastβ range of characters to appendstrβ string to appendsβ pointer to the character string to appendilistβ initializer list with the characters to appendtβ object convertible tostd::basic_string_viewwith the characters to append
π«Ά Return value π«Ά
*this
Bestie, that return value is why you can chain calls like it's nothing. Main character behavior, honestly.
π¦ Complexity π¦
There are no standard complexity guarantees, typical implementations behave similar to std::vector::insert(). So like⦠don't quote me on the exact Big-O, because the standard isn't giving us one.
π 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 β it has the strong exception safety guarantee. That's a very "we may have failed, but we did not leave a mess" moment.
π Example π
// Run this code
#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.
- LWG 847 β applied to C++98; behavior as published: there was no exception safety guarantee; correct behavior: added strong exception safety guarantee.
- LWG 2946 β applied to C++17; behavior as published: overload (8) causes ambiguity in some cases; correct behavior: avoided by making it a template.
π 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)
And that's the tea, bestie π: the source is http://cppreference.com, the page version/date is 2024.06.10, and the footer title is std::basic_string::append(3). Go forth and append responsibly β or at least with main character energy. πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms