Linux 拨号vps windows公众号手机端

c++拼接字符串的方式有哪些

lewis 7年前 (2018-03-16) 阅读数 6 #程序编程
文章标签 c++

在C++中,拼接字符串的方式有以下几种:

  1. 使用"+"运算符
std::string str1 = "Hello";
std::string str2 = "World";
std::string result = str1 + " " + str2; // result为"Hello World"
  1. 使用append()函数
std::string str1 = "Hello";
std::string str2 = "World";
str1.append(" ").append(str2); // str1为"Hello World"
  1. 使用stringstream
std::string str1 = "Hello";
std::string str2 = "World";
std::stringstream ss;
ss << str1 << " " << str2;
std::string result = ss.str(); // result为"Hello World"
  1. 使用std::ostringstream
std::string str1 = "Hello";
std::string str2 = "World";
std::ostringstream oss;
oss << str1 << " " << str2;
std::string result = oss.str(); // result为"Hello World"

这些是一些常用的拼接字符串的方式,根据具体情况选择合适的方式。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门