📜  使用 (str += s) 和 (str = str + s) 连接字符串之间的区别

📅  最后修改于: 2022-05-13 01:55:25.827000             🧑  作者: Mango

使用 (str += s) 和 (str = str + s) 连接字符串之间的区别

字符串是字符的集合。例如,“GeeksforGeeks”是一个字符串。 C++ 提供原始数据类型来创建字符串。字符串也可以在声明时初始化。

句法:

本文展示了使用加法赋值运算符(+=) 连接字符串与使用字符串的加法 (+)运算符之间的区别。连接是端到端连接的过程。

加法赋值 (+=)运算符

在 C++ 中,字符串加法赋值运算符用于将一个字符串连接到另一个字符串的末尾。

句法:

它将值(字面量)附加到字符串的末尾,而不进行任何重新分配。

示例:下面是演示加法赋值运算符的 C++ 程序。

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    str += str1;
 
    // Print the string
    cout << str;
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Driver code
public static void main(String[] args)
{
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    str += str1;
 
    // Print the String
    System.out.print(str);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python code for the above approach
# Driver code
 
# Declaring an empty string
str = "Geeks";
 
# String to be concatenated
str1 = "forGeeks";
 
# Concatenate str and str1
# using addition assignment operator
str += str1;
 
# Print the string
print(str);
 
# This code is contributed by gfgking


C#
// C# program to implement
// the above approach
using System;
 
public class GFG{
 
// Driver code
public static void Main(String[] args)
{
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    str += str1;
 
    // Print the String
    Console.Write(str);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the string
    cout << str;
    return 0;
}


Java
// Java program to implement
// the above approach
 
class GFG{
 
// Driver code
public static void main(String[] args)
{
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the String
    System.out.print(str);
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
public class GFG {
 
  // Driver code
  public static void Main(String[] args) {
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the String
    Console.Write(str);
  }
}
 
// This code is contributed by umadevi9616


C++
#include 
using namespace std;
 
int main()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    // Concatenate str1 at the end of str
    str += str1;
 
    // Print the string
    cout << "Resultant string using += "
         << str << '\n';
 
    str = "Geeks";
 
    // Concatenate str and str1
    // using addition operator
    // Concatenate str and str1
    // and assign the result to str again
    str = str + str1;
 
    // Print the string
    cout << "Resultant string using + "
         << str;
    return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "GeeksforGeeks";
 
    // String to be concatenated
    string str1 = " GeeksforGeeks";
 
    // String to be concatenated
    string str2 = " GeeksforGeeks";
 
    // String to be concatenated
    string str3 = " GeeksforGeeks";
 
    // Concatenate str, str1, str2 and str3
    // using addition assignment operator
    // in multiple statements
    str += str1;
 
    str += str2;
 
    str += str3;
 
    // Print the string
    cout << "Resultant string using +="
         << str << '\n';
 
    str = "GeeksforGeeks";
 
    // Concatenate str, str1, str and str3
    // using addition operator
    // in a single statement
    str = str + str1 + str2 + str3;
 
    // Print the string
    cout << "Resultant string using + "
         << str;
    return 0;
}


C++
// C++ program to calculate
// performance of +=
#include 
#include 
using namespace std;
 
// Function whose time is to
// be measured
void fun()
{
    // Initialize a n empty string
    string str = "";
 
    // concatenate the characters
    // from 'a' to 'z'
    for (int i = 0; i < 26; i++) {
        char c = 'a' + i;
        str += c;
    }
}
 
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
 
    // Start timer
    gettimeofday(&start, NULL);
 
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
 
    // Function Call
    fun();
 
    // Stop timer
    gettimeofday(&end, NULL);
 
    // Calculating total time taken
    // by the program.
    double time_taken;
 
    time_taken = (end.tv_sec
                  - start.tv_sec)
                 * 1e6;
 
    time_taken = (time_taken
                  + (end.tv_usec
                     - start.tv_usec))
                 * 1e-6;
 
    cout << "Time taken by program is : "
         << fixed
         << time_taken << setprecision(6);
    cout << " sec" << endl;
    return 0;
}


C++
// C++ program to calculate
// performance of +
#include 
#include 
using namespace std;
 
// Function whose time is to
// be measured
void fun()
{
    // Initialize a n empty string
    string str = "";
 
    // concatenate the characters
    // from 'a' to 'z'
    for (int i = 0; i < 26; i++) {
 
        char c = 'a' + i;
        str = str + c;
    }
}
 
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
 
    // Start timer
    gettimeofday(&start, NULL);
 
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
 
    // Function Call
    fun();
 
    // Stop timer
    gettimeofday(&end, NULL);
 
    // Calculating total time taken
    // by the program.
    double time_taken;
 
    time_taken = (end.tv_sec
                  - start.tv_sec)
                 * 1e6;
 
    time_taken = (time_taken
                  + (end.tv_usec
                     - start.tv_usec))
                 * 1e-6;
 
    cout << "Time taken by program is : "
         << fixed
         << time_taken << setprecision(6);
    cout << " sec" << endl;
    return 0;
}


输出
GeeksforGeeks

加法 (+)运算符

在 C++ 中,字符串加法运算符用于将一个字符串连接到另一个字符串的末尾。但在这种情况下,在字符串连接之后,修改后的字符串被分配给字符串。

句法:

它首先将值 (字面量) 附加到字符串的末尾,然后将其重新分配给 str。

示例:下面是演示上述方法的 C+ 程序。

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the string
    cout << str;
    return 0;
}

Java

// Java program to implement
// the above approach
 
class GFG{
 
// Driver code
public static void main(String[] args)
{
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the String
    System.out.print(str);
}
}
 
// This code is contributed by 29AjayKumar

C#

// C# program to implement
// the above approach
using System;
public class GFG {
 
  // Driver code
  public static void Main(String[] args) {
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the String
    Console.Write(str);
  }
}
 
// This code is contributed by umadevi9616
输出
GeeksforGeeks

虽然这两个运算符在与字符串一起使用时都可以用于字符串的连接,但它们之间存在一些差异:

因素 1:修改后的字符串的分配:

  • 加法赋值运算符(+=) 通过将一个字符串附加到另一个字符串的末尾来连接两个字符串。
  • 加法运算符(+) 通过在原始字符串的末尾附加一个字符串,然后将修改后的字符串分配给原始字符串。

示例:下面是演示上述方法的 C++ 程序。

C++

#include 
using namespace std;
 
int main()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    // Concatenate str1 at the end of str
    str += str1;
 
    // Print the string
    cout << "Resultant string using += "
         << str << '\n';
 
    str = "Geeks";
 
    // Concatenate str and str1
    // using addition operator
    // Concatenate str and str1
    // and assign the result to str again
    str = str + str1;
 
    // Print the string
    cout << "Resultant string using + "
         << str;
    return 0;
}
输出
Resultant string using += GeeksforGeeks
Resultant string using + GeeksforGeeks

因素 2:使用的运算符重载函数:

  • 加法赋值运算符(+=) 连接两个字符串,因为该运算符在内部被重载。
  • 在这种情况下,加法运算符(+) 也会连接两个字符串,因为该运算符在内部被重载。

因素 3:连接的字符串数:

  • 加法赋值运算符(+=) 可以在单个语句中一次连接两个字符串。
  • 加法运算符(+) 可以通过在单个语句中的字符串之间使用多个加法 (+)运算符来连接多个字符串。例如,str = str 1 + str 2 + str 3 + … + str n

示例:在这个程序中,需要三个不同的语句来连接三个字符串; str、str1、str2 和 str3 使用赋值加法运算符(+=) 并且需要一条语句来连接三个字符串; str、str1、str2 和 str3 使用加法运算符(+)。

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "GeeksforGeeks";
 
    // String to be concatenated
    string str1 = " GeeksforGeeks";
 
    // String to be concatenated
    string str2 = " GeeksforGeeks";
 
    // String to be concatenated
    string str3 = " GeeksforGeeks";
 
    // Concatenate str, str1, str2 and str3
    // using addition assignment operator
    // in multiple statements
    str += str1;
 
    str += str2;
 
    str += str3;
 
    // Print the string
    cout << "Resultant string using +="
         << str << '\n';
 
    str = "GeeksforGeeks";
 
    // Concatenate str, str1, str and str3
    // using addition operator
    // in a single statement
    str = str + str1 + str2 + str3;
 
    // Print the string
    cout << "Resultant string using + "
         << str;
    return 0;
}
输出
Resultant string using +=GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Resultant string using + GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks

因素 4:性能:

  • 加法赋值运算符(+=) 用于字符串连接时,与加法 (+)运算符相比,效率更高。这是因为在这种情况下不会重新分配字符串。
  • 加法运算符(+) 用于字符串连接时,与加法 (+=)运算符相比效率较低。这是因为在这种情况下会发生字符串的分配。

示例:下面是演示 +=字符串连接方法的性能的程序。

C++

// C++ program to calculate
// performance of +=
#include 
#include 
using namespace std;
 
// Function whose time is to
// be measured
void fun()
{
    // Initialize a n empty string
    string str = "";
 
    // concatenate the characters
    // from 'a' to 'z'
    for (int i = 0; i < 26; i++) {
        char c = 'a' + i;
        str += c;
    }
}
 
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
 
    // Start timer
    gettimeofday(&start, NULL);
 
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
 
    // Function Call
    fun();
 
    // Stop timer
    gettimeofday(&end, NULL);
 
    // Calculating total time taken
    // by the program.
    double time_taken;
 
    time_taken = (end.tv_sec
                  - start.tv_sec)
                 * 1e6;
 
    time_taken = (time_taken
                  + (end.tv_usec
                     - start.tv_usec))
                 * 1e-6;
 
    cout << "Time taken by program is : "
         << fixed
         << time_taken << setprecision(6);
    cout << " sec" << endl;
    return 0;
}
输出
Time taken by program is : 0.000046 sec

示例:下面是演示 +字符串连接方法的性能的程序。

C++

// C++ program to calculate
// performance of +
#include 
#include 
using namespace std;
 
// Function whose time is to
// be measured
void fun()
{
    // Initialize a n empty string
    string str = "";
 
    // concatenate the characters
    // from 'a' to 'z'
    for (int i = 0; i < 26; i++) {
 
        char c = 'a' + i;
        str = str + c;
    }
}
 
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
 
    // Start timer
    gettimeofday(&start, NULL);
 
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
 
    // Function Call
    fun();
 
    // Stop timer
    gettimeofday(&end, NULL);
 
    // Calculating total time taken
    // by the program.
    double time_taken;
 
    time_taken = (end.tv_sec
                  - start.tv_sec)
                 * 1e6;
 
    time_taken = (time_taken
                  + (end.tv_usec
                     - start.tv_usec))
                 * 1e-6;
 
    cout << "Time taken by program is : "
         << fixed
         << time_taken << setprecision(6);
    cout << " sec" << endl;
    return 0;
}
输出
Time taken by program is : 0.000034 sec
S No.Factor+= operator + operator
1 AssignmentIt 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.
2Overloaded functionsoperator overloaded function used with strings is different from the += operator. operator overloaded function used with strings is different from the + operator. 
3Number of strings concatenatedIt 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
4PerformanceThis 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.