在C++中,可以使用+运算符来拼接字符串。可以使用+运算符来拼接两个字符串或将字符串与其他数据类型的数据进行拼接。

例如:

#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";
    std::string result = str1 + " " + str2;
    std::cout << result << std::endl;
    
    return 0;
}

输出结果为:

Hello World

在上面的示例中,使用+运算符将str1str2拼接成了一个新的字符串result。在拼接过程中,可以使用+运算符将字符串与其他数据类型的数据进行拼接,例如字符串与整数、浮点数等。

#include <iostream>
#include <string>

int main() {
    std::string str = "Number: ";
    int num = 10;
    std::string result = str + std::to_string(num);
    std::cout << result << std::endl;
    
    return 0;
}

输出结果为:

Number: 10

在上面的示例中,使用std::to_string()函数将整数num转换为字符串,然后使用+运算符将字符串与转换后的整数拼接成了一个新的字符串result

c++拼接字符串

原文地址: https://www.cveoy.top/t/topic/jf4W 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录