C++ 字符串拼接操作符 += 和 + 的区别

C++ 字符串拼接操作符 += 和 + 的区别

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

语法:

string str;

string str = “GeeksforGeeks”

这里,“GeeksforGeeks” 是一个字符串常量。

本文介绍了使用加法分配运算符 (+=) 和字符串使用加法 (+) 运算符拼接字符串的区别。 拼接是将两个字符串依次连接在一起的过程。

加法分配 (+=) 运算符

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

语法:

str += value

这里,

value 是要与 str 连接的字符串。

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

示例: 以下是演示加法分配运算符的 C++ 程序。

// C++ program to implement
// the above approach
#include <iostream>
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;
}
C++
// 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
C++
# 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
C++
<script>
        // JavaScript code for the above approach
        // Driver code
 
        // Declaring an empty string
        let str = "Geeks";
 
        // String to be concatenated
        let str1 = "forGeeks";
 
        // Concatenate str and str1
        // using addition assignment operator
        str += str1;
 
        // Print the string
        document.write(str);
 
  // This code is contributed by Potta Lokesh
    </script> 
C++

输出

GeeksforGeeks
C++

加法(+)运算符

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

语法:

str = str + value

这里,

value是要与str连接的字符串。

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

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

// C++ program to implement
// the above approach
# include <iostream>
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;
}
C++
// 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++
# Python program to implement
# the above approach
 
# Driver code
 
# Declaring an empty string
str1 = "Geeks"
 
# String to be concatenated
str2 = "forGeeks"
 
# Concatenate str and str1
# using addition operator
str1 += str2
 
# Print the string
print(str1)
 
# This code is contributed by phasing17
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++
// JavaScript program to implement
// the above approach
 
// Driver code
function main() {
    // Declaring an empty string
    let str = "Geeks";
 
    // String to be concatenated
    let str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the string
    console.log(str);
}
 
main();
C++

输出

GeeksforGeeks
C++

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

因素1: 修改后的字符串的赋值:

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

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

#include <iostream>
using namespace std;
 
class StringConcat {
    public:
        string str;
        StringConcat operator + (const StringConcat& str1) {
            StringConcat result;
            result.str = str + str1.str;
            return result;
        }
};
 
int main()
{
    // Declaring string 1
    StringConcat str1;
    str1.str = "Geeks";
 
    // Declaring string 2
    StringConcat str2;
    str2.str = "forGeeks";
 
    // Declared string 3
    StringConcat str3;
 
    // Concatenating using overloaded operator
    str3 = str1 + str2;
 
    // Displaying concatenated strings
    cout << "The concatenated string using Operator overloading : "
         << str3.str;
    return 0;
}  
C++

Python 3:

# Python 3 code to demonstrate Operator
# Overloading
# Declaring String class
 
class String:
 
    # Initializing
    def __init__(self, string):
        self.string = string
         
    # Overloading the + operator
    def __add__(self, other):
        return self.string + other.string
 
# Driver code
 
# Creating 2 String class objects
Ob1 = String('GeeksforGeeks')
Ob2 = String(', I am from Python')
 
# Concatenating the 2 objects
Ob3 = Ob1 + Ob2
 
# Printing the concatenated string
print(Ob3) 
C++

C#:

using System;
 
class OperatorOverloadingDemo {
    public static void Main()
    {
        // Creating object of String class
        StringConcat str1 = new StringConcat();
        str1.str = "Geeks";
 
        // Creating object of String class
        StringConcat str2 = new StringConcat();
        str2.str = "forGeeks";
 
        // Creating object of String class
        StringConcat str3 = new StringConcat();
 
        // Concatenating two strings
        str3 = str1 + str2;
 
        // Displaying the resultant string
        Console.WriteLine("Resultant String : " + str3.str);
    }
}
 
public class StringConcat {
    public String str;
 
    // Defining the overloaded + operator
    public static StringConcat operator +(StringConcat s1, StringConcat s2)
    {
        StringConcat s = new StringConcat();
        s.str = s1.str + s2.str;
        return s;
    }
}  
C++

Java:

class StringConCat {
    String str;
     
    public StringConCat(String str) {
        this.str = str;
    }
 
    public String toString() {
        return str;
    }
 
    // Overloading ‘+’ operator for adding two objects
    public StringConCat operator+ (StringConCat s) {
        String str = this.str + s.str;    // Concatenating the two strings
        return new StringConCat(str);
    }
}
 
class Main {
    public static void main(String[] args) {
        StringConCat s1 = new StringConCat("Geeks");
        StringConCat s2 = new StringConCat("forGeeks");
        StringConCat s3 = s1 + s2; // Concatenating the objects using ‘+’ operator
 
        System.out.println("Concatenated String : " + s3.toString());    
    }
}  
C++

输出

The concatenated string using Operator overloading : GeeksforGeeks
C++
  • 加法赋值运算符(+=)将两个字符串连接起来,因为运算符在内部被重载。
  • 在这种情况下,加法运算符(+)也将两个字符串连接起来,因为运算符在内部被重载。

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

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

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

// C++程序实现
// 上面的方法
# include <iostream>
using namespace std;
 
// 驱动代码
int main()
{
 
    // 声明空字符串
    string str = "GeeksforGeeks";
 
    // 要连接的字符串
    string str1 = " GeeksforGeeks";
 
    // 要连接的字符串
    string str2 = " GeeksforGeeks";
 
    // 要连接的字符串
    string str3 = " GeeksforGeeks";
 
    // 使用赋加运算符
    // 在多个语句中连接str、str1、str2和str3
    str += str1;
 
    str += str2;
 
    str += str3;
 
    // 打印字符串
    cout << "Resultant string using +="
         << str << '\n';
 
    str = "GeeksforGeeks";
 
    // 使用加法运算符
    // 在单个语句中连接str、str1、str和str3
    str = str + str1 + str2 + str3;
 
    // 打印字符串
    cout << "Resultant string using + "
         << str;
    return 0;
}
C++
// Java程序实现
// 上面的方法
import java.io.*;
 
class GFG {
 
// 驱动代码
public static void main (String[] args)
{
 
    // 声明空字符串
    String str = "GeeksforGeeks";
 
    // 要连接的字符串
    String str1 = " GeeksforGeeks";
 
    // 要连接的字符串
    String str2 = " GeeksforGeeks";
 
    // 要连接的字符串
    String str3 = " GeeksforGeeks";
 
    // 使用赋加运算符
    // 在多个语句中连接str、str1、str2和str3
    str += str1;
 
    str += str2;
 
    str += str3;
 
    // 打印字符串
    System.out.println("Resultant string using +="
         +str);
 
    str = "GeeksforGeeks";
 
    // 使用加法运算符
    // 在单个语句中连接str、str1、str和str3
    str = str + str1 + str2 +str3;
 
    // 打印字符串
    System.out.print("Resultant string using + "
         + str);
}
}
C++

输出结果

Resultant string using +=GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Resultant string using + GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
C++

因素4: 性能:

  • 当进行字符串连接时,赋值加法运算符(+=)比加法运算符(+​​)效率更高。这是因为在此情况下不会重新分配字符串。
  • 当进行字符串连接时,加法运算符(+)比赋值加法运算符(+=)效率更低。这是因为在此情况下进行字符串赋值。

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

// C ++程序计算
// +=
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
 
// 测量时间的函数
void fun()
{
    // 初始化一个空字符串
    string str = "";
 
    // 连接字符
    // 从'a'到'z'的
    for (int i = 0; i < 26; i++) {
        char c = 'a' + i;
        str += c;
    }
}
 
// 驱动器代码
int main()
{
    // 使用函数gettimeofday()
    // 可以获得时间
    struct timeval start, end;
 
    // 开始计时器
    gettimeofday(&start, NULL);
 
    // C和C ++语言的I / O不同步。
    ios_base::sync_with_stdio(false);
 
    // 调用功能
    fun();
 
    // 停止计时器
    gettimeofday(&end, NULL);
 
    // 计算程序花费的总时间。
    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 << "程序需要的时间是:"
         << fixed
         << time_taken << setprecision(6);
    cout << "秒" << endl;
    return 0;
}  
C++

输出

程序需要的时间是:0.000046
C++

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

// C ++程序计算
// +
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
 
// 测量时间的函数
void fun()
{
    // 初始化一个空字符串
    string str = "";
 
    // 连接字符
    // 从'a'到'z'的
    for (int i = 0; i < 26; i++) {
        char c = 'a' + i;
        str = str + c;
    }
}
 
// 驱动器代码
int main()
{
    // 使用函数gettimeofday()
    // 可以获得时间
    struct timeval start, end;
 
    // 开始计时器
    gettimeofday(&start, NULL);
 
    // C和C ++语言的I / O不同步。
    ios_base::sync_with_stdio(false);
 
    // 调用功能
    fun();
 
    // 停止计时器
    gettimeofday(&end, NULL);
 
    // 计算程序花费的总时间。
    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 <&< "程序需要的时间是:"
         << fixed
         << time_taken << setprecision(6);
    cout << "秒" << endl;
    return 0;
}  
C++

输出

程序需要的时间是:0.000034
C++
序号 因素 += 运算符 + 运算符
1 赋值 它将字符串附加到原始字符串的末尾。 它将字符串附加到原始字符串的末尾,然后将修改后的字符串重新赋值给原始字符串。
2 重载函数 用于字符串的重载函数与 += 运算符不同。 用于字符串的重载函数与 + 运算符不同。
3 连接的字符串数量 它可以在单个语句中同时连接两个字符串。 可以使用多个加号(+)运算符在多个字符串之间连接多个字符串。例如,str = str1 + str2 + str3 + … + str n
4 性能 当用于字符串连接时,此运算符比加法(+)运算符效率更高。这是因为在这种情况下不会重新分配字符串。 当用于字符串连接时,此运算符不如加法(+=)运算符高效。这是因为在这种情况下重新分配字符串。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册