这两种添加字符串的情况有什么区别? [英] What is the difference between these two cases of adding a string?
问题描述
我注意到初始化字符串时,编译器报告了一个我没有想到的错误.
I noticed that when I initialized a string, the compiler reported an error that I was not expecting.
例如:
For example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Hello", s2 = "World!"; // ok
string s3 = s1 + ", " + "World!"; // ok
string s4 = "Hello" + ", " + s2; // error
cout << s1 + " " + s2 << endl; //ok
return 0;
}
对我来说,如果 s3
工作得很好,那么 s4
也应该做同样的事情.
For me, if s3
worked just fine, s4
should do the same.
为什么会出现该错误?这两个初始化字符串( s3
和 s4
)有什么区别?
Why do I get that error? What is the difference between these two initialization strings (s3
and s4
)?
推荐答案
你好"
不是 std :: string
(而是 constchar [6]
,而,"
是 const char [3]
)和的
s不适用. +
运算符std :: string
"Hello"
is not a std::string
(rather it is a const char[6]
, while ", "
is a const char[3]
) and the +
operator for std::string
s does not apply.
这是C ++的一个小麻烦,源于其C祖先.这意味着在通过 +
连接字符串时,必须确保其两个操作数中的至少一个实际上是一个 std :: string
,如下所示:
This is a minor inconvenience of C++ and stems from its C ancestry. It means that in concatenating strings via +
you must ensure that at least one of its two operands actually is a std::string
, as in
auto s = std::string{"Hello"} + ", " + "World";
其中第一个 +
具有一个 std :: string
作为其第一个操作数,并因此生成一个 std :: string
,因此第二个 +
也有一个 std :: string
作为其第一个操作数(因为从左到右处理了 +
).
where the first +
has a std::string
as its first operand and hence produces a std::string
, so that the second +
also has a std::string
as its first operand (since +
is processed from left to right).
Edit1 由 TC 的注释提示,我提到C字符串文字是如果仅用空格隔开,则会自动串联:
Edit1 prompted by the comment by T.C., I mention that C-string literals are automatically concatenated if only separated by white space:
std::string s = "Hello" ", " "World";
此行为也继承自C:预处理器将上述代码呈现给
This behaviour too is inherited from C: the preprocessor renders above code to
std::string s = "Hello, World";
在编译器正确处理之前(更准确地说,字符串连接发生在翻译的第6阶段,就在编译之前).实际上,这是连接原始字符串文字的最简单,因此也是最方便的方法.但是请注意,我必须声明 s
的类型,因为 auto
推导会给出一个 const char *
.
before the compiler proper processes it (to be more precise, string concatenation takes place in phase 6 of translation, just before compilation). This is in fact the most simple and hence most convenient way to concatenate raw string literals. But note that I had to declare the type of s
, since auto
deduction would have given a const char*
.
Edit2 由 PaperBirdMaster 的注释提示,我提到自C ++ 14起,您只需在字符串后加上 s
即可直接形成 std :: string
文字, if 插入关联的
Edit2 prompted by the comment by PaperBirdMaster, I mention that since C++14, you can directly form std::string
literals by simply adding s
after the string, if you pull in the associated operator""s
(or surrounding namespace).
using std::literals::operator""s; // pull in required operator
const auto s = "Hello"s + ", " + "World"; // std::string
请参阅这篇文章,以了解为什么所需的运算符"的
隐藏在嵌套名称空间中.还请注意,除了使用std :: literals :: operator" s; 进行替代之外,您还可以拉入周围的
namespace
:以下两种声明均可.
See this post as to why the required operator""s
is hidden in the nested namespace. Note also that alternatively to using std::literals::operator""s;
you may pull in the surrounding namespace
: either of the following declarations will do.
using namespace std::string_literals;
using namespace std::literals::string_literals;
using namespace std::literals;
使用命名空间std; 不如普通的糟糕(可恶).
which are not as bad (and damned) as plain using namespace std;
.
这篇关于这两种添加字符串的情况有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!