std::basic_string::operator+= - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::operator+=

From cppreference.com
< cpp‎ | string‎ | basic string

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)

Appends addinional 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`.

### [edit] Parameters

ch | - | character value to append
str | - | string to append
s | - | pointer to a null-terminated character string to append
init | - | initializer list with the characters to append

### [edit] Return value

*this

### [edit] Complexity

1) linear in size of `str`

2) constant

3) linear in size of `s`

4) linear in size of `init`

### [edit] 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.

### [edit] Example

Run this code

    #include <iostream>
    #include <string>

    int main()
    {
       std::string str;
       str.reserve(50); //reserves sufficient storage space to avoid memory reallocation
       std::cout << str << '\n'; //empty string

       str += "This";
       std::cout << str << '\n';

       str += std::string(" is ");
       std::cout << str << '\n';

       str += 'a';
       std::cout << str << '\n';

       str += {' ','s','t','r','i','n','g','.'};
       std::cout << str << '\n';
    }

Output:

    This
    This is
    This is a
    This is a string.

### [edit] See also

assign | assign characters to a string (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator%2B%3D&oldid=67193"