使用 (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!
“+=”和“+”運算子的區別
現在,讓我們討論在使用“+=”和“+”運算子連線字串時的區別。
“+=”運算子 |
“+”運算子 |
|
|---|---|---|
字串數量 |
它在一個語句中只能連線兩個字串。 |
它在一個語句中可以連線兩個或多個字串。 |
字串賦值 |
它將字串附加到原始字串。 |
它將字串連線到原始字串並重新賦值字串。 |
效能 |
“+=”的效能優於“+”運算子,因為它將字串附加到原始字串。 |
由於字串連線後重新賦值字串,因此其效能較差。 |
示例
下面的示例演示了“+=”和“+”運算子之間的區別。在這裡,我們建立了四個不同的字串。此外,我們還定義了“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
示例
在這種方法中,我們測量“+=”和“+”運算子的效能。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 個字串時,我們應該使用“+”運算子以使程式碼更易讀。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP