![]() |
VOOZH | about |
A string is a collection of characters. For example, "GeeksforGeeks" is a string. C++ provides primitive data types to create a string. The string can also be initialized at the time of declaration.
Syntax:
string str;
string str = "GeeksforGeeks"
Here, "GeeksforGeeks" is a string literal.
This article shows the difference between the concatenation of the strings using the addition assignment operator (+=) and the addition (+) operator used with strings. Concatenation is the process of joining end-to-end.
In C++, a string addition assignment operator is used to concatenate one string to the end of another string.
Syntax:
str += value
Here,
value is a string to be concatenated with str.
It appends the value (literal) at the end of the string, without any reassignment.
Example: Below is the C++ program to demonstrate the addition assignment operator.
GeeksforGeeks
In C++, a string addition operator is used to concatenate one string to the end of another string. But in this case the after the concatenation of strings, the modified string gets assigned to the string.
Syntax:
str = str + value
Here,
value is a string to be concatenated with str.
It firstly appends the value (literal) at the end of the string and then reassigns it to str.
Example: Below is the C+ program to demonstrate the above approach.
GeeksforGeeks
Although both operators when used with strings can be used for the concatenation of strings, there are some differences between them:
Factor 1: Assignment of the modified string:
Example: Below is the C++ program to demonstrate the above approach.
Resultant string using += GeeksforGeeks Resultant string using + GeeksforGeeks
Factor 2: Operator overloaded functions used:
Factor 3: Number of strings concatenated:
Example: In this program, three different statements are required to concatenate three strings; str, str1, str2, and str3 using the assignment addition operator (+=) and a single statement is required to concatenate three strings; str, str1, str2, and str3 using the addition operator (+).
Resultant string using +=GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks Resultant string using + GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Factor 4: Performance:
Example: Below is the program to demonstrate the performance of the += string concatenation method.
Time taken by program is : 0.000490 sec
Example: Below is the program to demonstrate the performance of the + string concatenation method.
Time taken by program is : 0.000715 sec
| S No. | Factor | += operator | + operator |
| 1 | Assignment | It appends a string at the end of the original string. | It appends a string at the end of the original string and then reassigns the modified string to the original string. |
| 2 | Overloaded functions | operator overloaded function used with strings is different from the += operator. | operator overloaded function used with strings is different from the + operator. |
| 3 | Number of strings concatenated | It can concatenate two strings at a time in a single statement. | Multiple strings can be concatenated using multiple addition (+) operators between the string. For example, str = str1 + str2 + str3 + ... + strn |
| 4 | Performance | This operator when used for the concatenation of strings gives better efficiency as compared to the addition(+) operator. This is because no reassignment of strings takes place in this case. | This operator when used for the concatenation is not as efficient as compared to the addition(+=) operator. This is because reassignment of strings takes place in this case. |