C++20 sections · 1024 units
Open in Course

The += Operator

Adding to existing strings

The += operator appends text to an existing string. Write s += "more"; to add to the end. This modifies s directly rather than creating a new string. More efficient for repeated additions.

Use += when building incrementally. In a loop, result += nextPart; adds each piece to the growing result. Each append may reallocate, but the class optimizes with extra capacity.

You can append variables, literals, or single characters. s += '!'; appends one character. s += suffix; appends another string. All forms modify the left-hand string.