basic_string& operator+=( const basic_string& str ); | (1) | |
basic_string& operator+=( CharT ch ); | (2) | |
basic_string& operator+=( const CharT* s ); | (3) | |
basic_string& operator+=( std::initializer_list<CharT> ilist ); | (4) | (since C++11) |
basic_string& operator+=( std::basic_string_view<CharT, Traits> sv); | (5) | (since C++17) |
Appends additional characters to the string.
str
ch
s
. ilist
.sv
as if by append(sv)
str | - | string to append |
ch | - | character value to append |
s | - | pointer to a null-terminated character string to append |
ilist | - | std::initializer_list with the characters to append |
sv | - | std::basic_string_view with the characters to append |
*this
.
str
s
ilist
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
.
Owing to Implicit conversions, operator+=
might accept values of unwanted types.
#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"
assign characters to a string (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/operator+=