
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between String Concatenation Using str + s and str + s
In this tutorial, we will learn the difference between the concatenation of string using the ?+=' or ?+' operator. Both operators are used to merge strings, but we learn some differences below with examples.
What is Addition Assignment (+=) operator?
The addition assignment (+=) operator concatenates the two strings. It takes two operands. The left operand is the original string, and the right operand is a string we need to concatenate with the original string. Generally, we use the ?+=' operator when we require to concatenate only two strings.
Syntax
Users can follow the syntax below to use the addition assignment (+=) operator to concatenate the string.
Str += str1;
In the above syntax, Str is an original string, and str1 is the string to concatenate to Str.
Example
In the example below, we defined two strings named str1 and str2. After that, we used the addition assignment operator to merge string str1 with str2. Programmers can observe the output of the below code, which concatenates the given string.
#include <bits/stdc++.h> using namespace std; int main() { // First string string str1 = "TutorialsPoint"; // second string string str2 = " is a best website!"; // Using += operator to concatenate str1 += str2; cout << "The updated str1 is " << str1; return 0; }
Output
The updated str1 is TutorialsPoint is a best website!
What is the Addition (+) operator?
In C++, the addition (+) operator is also used to concatenate two strings, like adding two number values. It appends the string to the original string, and we can assign the resultant string to the third string. We can use the ?+' operator for string concatenation when we require to concatenate more than two strings in a single statement.
Syntax
Users can follow the syntax below to use the addition operator to concatenate the strings.
string s = str1 + str2;
In the above syntax, we concatenate the str1 and str2 strings and assign the resultant string to the ?s' string.
Example
In the example below, the str1 and str2 string variable contains different string values. After that, we merged str1 and str2, assigning the resultant string to str1. In the output, we can observe the value of the str1 string.
#include <bits/stdc++.h> using namespace std; int main() { // First string string str1 = "TutorialsPoint"; // second string string str2 = " is a best website!"; // Using += operator to concatenate str1 = str1 + str2; cout << "The updated str1 is " << str1; return 0; }
Output
The updated str1 is TutorialsPoint is a best website!
Difference between ?+=' and ?+' operator
Now, let's discuss the difference between the ?+=' and ?+' operators when we use them to concatenate the string.
?+=' operator |
?+' operator |
|
---|---|---|
Number of string |
It can concatenate only two strings in a single statement. |
It can concatenate two or more strings in a single statement. |
String assignment |
It appends the string to the original string. |
It concatenates the string to the original string and reassigns the string. |
Performance |
The performance of ?+=' is better than the ?+' operator as it appends the string to the original string. |
It performs poorly due to the reassignment of the string after string concatenation. |
Example
The example below demonstrates the difference between the ?+=' and ?+' operators. Here, we created four different strings. Also, we have defined the ?result' string and used the ?+' operator to concatenate all four strings in the single statement and assign them to the ?result' string.
After that, we use the ?+=' operator to append all strings to the first. We have written 3 statements to append 3 strings to the ?first' string.
In the output, we can see that both approaches give the same output, but we need to write more statements while using the ?+=' operator/.
#include <bits/stdc++.h> using namespace std; int main(){ // Defining multiple strings string first = "C++"; string second = "Programming"; string third = "Language"; string fourth = "is awesome"; // Concatenating strings using + operator string result = first + " " + second + " " + third + " " + fourth; // print result cout << "Using the + operator - " << result << endl; // Use += operator to concat strings first += second; first += third; first += fourth; // print result cout << "Using the += operator - " << first << endl; return 0; }
Output
Using the + operator - C++ Programming Language is awesome Using the += operator - C++ProgrammingLanguageis awesome
Example
In this approach, we measure the performance of the ?+=' and ?+' operators. The addAssignOperator() function converts the 1 to 100000 positive integers in the string and appends to the ?alpha' string using the ?+=' operator. Similarly, the addOperator() function appends 1 to 100000 digits to the ?alpha' string using the ?+' operator.
In the main() method, we used the ?struct timeval' from the ?time.h' library to calculate the execution time of the function. We use the gettimeofday() method to get the current time. We execute it before and after the function call. Next, we can take a time difference to get the function execution time. In this way, we find the execution time for both functions.
In the output, we can observe that ?+=' is very faster than ?+' operator.
#include <bits/stdc++.h> #include <sys/time.h> using namespace std; // function to concat string using += operator void addAssignOperator() { // empty string string alpha = ""; // Append numbers for (int p = 0; p < 100000; p++) { alpha += to_string(p); } } // function to concat string using + operator void addOperator() { // empty string string alpha = ""; // Append numbers for (int p = 0; p < 100000; p++) { alpha = alpha + to_string(p); } } int main() { // variables to store startTime and endTime time struct timeval startTime, endTime; // Initialize start time gettimeofday(&startTime, NULL); ios_base::sync_with_stdio(false); addAssignOperator(); // store end time gettimeofday(&endTime, NULL); // to store total time taken by function addAssignOperator() double totalTime = ((endTime.tv_sec - startTime.tv_sec) * 1e6 + (endTime.tv_usec - startTime.tv_usec)) * 1e-6; cout << "Total time to use += operator is : " << totalTime << setprecision(10) << " secs" << endl; // Initialize start time gettimeofday(&startTime, NULL); ios_base::sync_with_stdio(false); addOperator(); // store end time gettimeofday(&endTime, NULL); // to store total time taken by function addOperator() totalTime = ((endTime.tv_sec - startTime.tv_sec) * 1e6 + (endTime.tv_usec - startTime.tv_usec)) * 1e-6; cout << "Total time to use + operator is : " << totalTime << setprecision(7) << " secs" << endl; return 0; }
Output
Total time to use += operator is : 0.005787 secs Total time to use + operator is : 0.624564 secs
Please note the above times may vary on each compilation.
When we require to concatenate the array of strings containing thousands of entries, we should use the ?+=' operator to get better performance, and when we require to use 2 to 5 strings, we should use the ?+' operator to make code more readable.