C++ 使用 (str += s) 和 (str = str + s) 拼接字符串的区别
在本教程中,我们将学习使用 ‘+=’ 或 ‘+’ 操作符进行字符串拼接的区别。这两个操作符都用于合并字符串,但是我们将通过以下示例来了解其中的一些区别。
什么是加法赋值 (+=) 操作符
加法赋值 (+=) 操作符将两个字符串连接起来。它有两个操作数。左操作数是原始字符串,右操作数是我们需要与原始字符串连接的字符串。通常,我们在只需要连接两个字符串时使用 ‘+=’ 操作符。
语法
用户可以按照以下语法使用加法赋值 (+=) 操作符来连接字符串。
Str += str1;
在上述语法中,Str是原始字符串,str1是要连接到Str的字符串。
示例
在下面的示例中,我们定义了两个字符串,名为str1和str2。然后,我们使用加法赋值运算符将字符串str1与str2合并。程序员可以观察下面代码的输出,它将给定的字符串连接起来。
#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;
}
输出
The updated str1 is TutorialsPoint is a best website!
什么是加法(+)操作符
在C++中,加法(+)操作符也可用于连接两个字符串,就像相加两个数值一样。它将字符串附加到原始字符串上,并且我们可以将结果字符串赋值给第三个字符串。当我们需要在单个语句中连接多个字符串时,我们可以使用‘+’操作符进行字符串连接。
语法
用户可以按照以下语法使用加法操作符来连接字符串。
string s = str1 + str2;
在上面的语法中,我们将str1和str2字符串连接起来,并将结果字符串赋值给’s’字符串。
例子
在下面的例子中,str1和str2字符串变量包含不同的字符串值。之后,我们合并了str1和str2,并将结果字符串赋值给str1。在输出中,我们可以观察到str1字符串的值。
#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;
}
输出
The updated str1 is TutorialsPoint is a best website!
‘+=’和‘+’运算符的区别
现在,让我们讨论一下当我们使用‘+=’和‘+’运算符进行字符串连接时的区别。
对比项 | ‘+=’操作符 | ‘+’操作符 |
---|---|---|
字符串数量 | 它只能在一条语句中连接两个字符串。 | 它可以在一条语句中连接两个或更多的字符串。 |
字符串赋值 | 它将字符串附加到原始字符串上。 | 它将字符串与原始字符串连接起来并重新赋值字符串。 |
性能 | ‘+=’的性能比’+’操作符更好,因为它将字符串附加到原始字符串上。 | 由于字符串连接后的重新赋值,所以性能较差。 |
示例1
下面的示例演示了“+=”和“+”运算符之间的区别。在这里,我们创建了四个不同的字符串。还定义了“result”字符串并使用“+”运算符将所有四个字符串连接在一个语句中,并将它们赋值给“result”字符串。
然后,我们使用“+=”运算符将所有字符串附加到第一个字符串上。我们编写了3个语句将3个字符串附加到“first”字符串上。
在输出中,我们可以看到两种方法产生相同的输出,但是使用“+=”运算符需要编写更多的语句。
#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;
}
输出
Using the + operator - C++ Programming Language is awesome
Using the += operator - C++ProgrammingLanguageis awesome
示例2
在这种方法中,我们测量了‘+=’和‘+’运算符的性能。addAssignOperator()函数将1到100000个正整数转换为字符串,并使用‘+=’运算符将其追加到‘alpha’字符串中。同样,addOperator()函数使用‘+’运算符将1到100000个数字追加到‘alpha’字符串中。
在main()方法中,我们使用来自‘time.h’库的‘struct timeval’来计算函数的执行时间。使用gettimeofday()方法获取当前时间,并在函数调用之前和之后执行它。然后,我们可以取时间差来得到函数的执行时间。通过这种方式,我们找到了两个函数的执行时间。
在输出中,我们可以观察到‘+=’比‘+’运算符快得多。
#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;
}
输出
Total time to use += operator is : 0.005787 secs
Total time to use + operator is : 0.624564 secs
请注意,上述时间可能在每次编译中有所不同。 当我们需要连接包含数千个条目的字符串数组时,应使用’+=’运算符以获得更好的性能;而当我们需要使用2到5个字符串时,应使用’+’运算符以使代码更易读。