c++中字符串能简单相加吗

2025-06-28 03:48:52
推荐回答(2个)
回答1:

可以的。例如:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

int main(){

string str1 = "hello ";
string str2 = "world.";
string str3 = str1 + str2;
cout << str3 << endl;

return 0;
}

回答2:

字符串字面值不可直接相加;
string a = "123"+"456";//error
string b = "123";
string c = b +"456";//ac
这还有人点灭? 上面那条回答一开始写的字面值直接相加 也就是string str1 ="hello "+"world." 被我纠正了还骂我 我已经删了评论了。现在他修改成变量相加了,真是笑死人,我说的不对么,偷偷摸摸改答案不承认自己的错误。踩的也是铁憨憨,自己试试都不会,sb