在C++中,拼接字符串的方式有以下几种:
std::string str1 = "Hello";
std::string str2 = "World";
std::string result = str1 + " " + str2; // result为"Hello World"
std::string str1 = "Hello";
std::string str2 = "World";
str1.append(" ").append(str2); // str1为"Hello World"
std::string str1 = "Hello";
std::string str2 = "World";
std::stringstream ss;
ss << str1 << " " << str2;
std::string result = ss.str(); // result为"Hello World"
std::string str1 = "Hello";
std::string str2 = "World";
std::ostringstream oss;
oss << str1 << " " << str2;
std::string result = oss.str(); // result为"Hello World"
这些是一些常用的拼接字符串的方式,根据具体情况选择合适的方式。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c++字符串拼接的方式有哪几种