【发布时间】:2020-09-19 17:18:40
【问题描述】:
我是 C++ 新手,我想知道这两行之间有什么区别:
cout << "John " << "Doe" << endl;
cout << "John " + "Doe" << endl;
第一个有效,第二个无效。有什么想法吗?
【问题讨论】:
-
第一个打印
John Doe...第二个,没有。您实际上是将两个数组添加在一起,这不会编译。 Try it and see -
您应该将
cout << "John "s + "Doe"s << endl;添加到您的问题中。这就是字符串文字的连接方式。 wandbox.org/permlink/WaW4Kx8LHXh5KdX6 -
都不连接字符串。第一个打印两个不同的字符串,它们之间没有任何输出。第二个不编译。
-
这并没有解决问题,但您不需要
std::endl所做的额外内容。'\n'结束一行。
标签: c++ string concatenation c-strings string-literals