std::string operator+=
- od C++20
- od C++17
- od C++11
- do C++11
// (1) Non const version only
constexpr basic_string& operator+=( const basic_string& str );
// (2) Non const version only
constexpr basic_string& operator+=( CharT ch );
// (3) Non const version only
constexpr basic_string& operator+=( const CharT* s );
// (4) Non const version only
constexpr basic_string& operator+=( std::initializer_list<CharT> ilist );
// (5) Non const version only
template < class StringViewLike >
constexpr basic_string& operator+=( const StringViewLike& t );
// (1) Non const version only
basic_string& operator+=( const basic_string& str );
// (2) Non const version only
basic_string& operator+=( CharT ch );
// (3) Non const version only
basic_string& operator+=( const CharT* s );
// (4) Non const version only
basic_string& operator+=( std::initializer_list<CharT> ilist );
// (5) Non const version only
template < class StringViewLike >
basic_string& operator+=( const StringViewLike& t );
// (1) Non const version only
basic_string& operator+=( const basic_string& str );
// (2) Non const version only
basic_string& operator+=( CharT ch );
// (3) Non const version only
basic_string& operator+=( const CharT* s );
// (4) Non const version only
basic_string& operator+=( std::initializer_list<CharT> ilist );
// (1) Non const version only
basic_string& operator+=( const basic_string& str );
// (2) Non const version only
basic_string& operator+=( CharT ch );
// (3) Non const version only
basic_string& operator+=( const CharT* s );
Appends additional characters to the string.
- (1) Appends string
str. - (2) Appends character
ch. - (3) Appends the null-terminated character string pointed to by
s. - (4) Appends characters in the initializer list
ilist. - (5) Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then appends characters in the string viewsvas if byappend(sv).Overload ResolutionThis overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>istrueandstd::is_convertible_v<const StringViewLike&, const CharT*>isfalse.
Parameters
str- string to appendch- character value to appends- pointer to a null-terminated character string to appendilist-std::initializer_listwith the characters to appendt- object (convertible tostd::basic_string_view) with the characters to append
Return value
*this
Complexity
important
This section requires improvement. You can help by editing this doc page.
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.
Notes
uwaga
Overload (2) can accept any types that are implicitly convertible to CharT.
For std::string, where CharT is char, the set of acceptable types includes all arithmetic types.
This may have unintended effects.
Example
Main.cpp
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string str;
str.reserve(50); //reserves sufficient storage space to avoid memory reallocation
std::cout << std::quoted(str) << '\n'; //empty string
str += "This";
std::cout << std::quoted(str) << '\n';
str += std::string(" is ");
std::cout << std::quoted(str) << '\n';
str += 'a';
std::cout << std::quoted(str) << '\n';
str += {' ','s','t','r','i','n','g','.'};
std::cout << std::quoted(str) << '\n';
str += 76.85; // equivalent to str += static_cast<char>(76.85), might not be the intent
std::cout << std::quoted(str) << '\n';
}
Output
""
"This"
"This is "
"This is a"
"This is a string."
"This is a string.L"
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.
Hover to see the original license.