C++ 连接两个字符串文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6061648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate two string literals
提问by Arthur Collé
I am reading Accelerated C++ by Koenig. He writes that "the new idea is that we can use + to concatenate a string and a string literal - or, for that matter, two strings (but not two string literals).
我正在阅读 Koenig 的 Accelerated C++。他写道,“新想法是我们可以使用 + 来连接一个字符串和一个字符串文字——或者,就此而言,两个字符串(但不是两个字符串文字)。
Fine, this makes sense I suppose. Now onto two separate exercises meant to illuminate this .
好吧,我想这是有道理的。现在进行两个单独的练习,旨在阐明这一点。
Are the following definitions valid?
以下定义是否有效?
const string hello = "Hello";
const string message = hello + ",world" + "!";
Now, I tried to execute the above and it worked! So I was happy.
现在,我尝试执行上述操作并且成功了!所以我很高兴。
Then I tried to do the next exercise;
然后我试着做下一个练习;
const string exclam = "!";
const string message = "Hello" + ",world" + exclam;
This did not work. Now I understand it has something to do with the fact that you cannot concatenate two string literals, but I don't understand the semantic difference between why I managed to get the first example to work (isn't ",world" and "!" two string literals? Shouldn't this not have worked?) but not the second.
这没有用。现在我明白这与您不能连接两个字符串文字这一事实有关,但我不明白为什么我设法让第一个示例工作(不是“,世界”和“! " 两个字符串字面量?这不应该起作用吗?)但不是第二个。
回答by James McNellis
const string message = "Hello" + ",world" + exclam;
The +
operator has left-to-right associativity, so the equivalent parenthesized expression is:
该+
运算符具有从左到右的结合性,因此等效的括号表达式为:
const string message = (("Hello" + ",world") + exclam);
As you can see, the two string literals "Hello"
and ",world"
are "added" first, hence the error.
如您所见,两个字符串文字"Hello"
和",world"
是首先“添加”的,因此出现错误。
One of the first two strings being concatenated must be a std::string
object:
连接的前两个字符串之一必须是std::string
对象:
const string message = string("Hello") + ",world" + exclam;
Alternatively, you can force the second +
to be evaluated first by parenthesizing that part of the expression:
或者,您可以+
通过将表达式的该部分括起来来强制首先计算第二个:
const string message = "Hello" + (",world" + exclam);
It makes sense that your first example (hello + ",world" + "!"
) works because the std::string
(hello
) is one of the arguments to the leftmost +
. That +
is evaluated, the result is a std::string
object with the concatenated string, and that resulting std::string
is then concatenated with the "!"
.
您的第一个示例 ( hello + ",world" + "!"
) 起作用是有道理的,因为std::string
( hello
) 是最左边的参数之一+
。即+
进行评估,结果是一个std::string
带有连接字符串的对象,然后将结果std::string
与"!"
.
As for whyyou can't concatenate two string literals using +
, it is because a string literal is just an array of characters (a const char [N]
where N
is the length of the string plus one, for the null terminator). When you use an array in most contexts, it is converted into a pointer to its initial element.
至于为什么不能使用 连接两个字符串文字+
,那是因为字符串文字只是一个字符数组(a const char [N]
whereN
是字符串的长度加一,对于空终止符)。在大多数情况下使用数组时,它会转换为指向其初始元素的指针。
So, when you try to do "Hello" + ",world"
, what you're really trying to do is add two const char*
s together, which isn't possible (what would it mean to add two pointers together?) and if it was it wouldn't do what you wanted it to do.
因此,当您尝试这样做时"Hello" + ",world"
,您真正想做的是将两个const char*
s 相加,这是不可能的(将两个指针相加在一起意味着什么?)想要它做。
Note that you canconcatenate string literals by placing them next to each other; for example, the following two are equivalent:
请注意,您可以通过将字符串文字并排放置来连接它们;例如,以下两个是等效的:
"Hello" ",world"
"Hello,world"
This is useful if you have a long string literal that you want to break up onto multiple lines. They have to be string literals, though: this won't work with const char*
pointers or const char[N]
arrays.
如果您想将一个长字符串文字分解为多行,这将非常有用。但是,它们必须是字符串文字:这不适用于const char*
指针或const char[N]
数组。
回答by Yochai Timmer
You should always pay attention to types.
您应该始终注意类型。
Although they all seem like strings, "Hello"
and ",world"
are literals.
尽管它们看起来都像字符串,"Hello"
并且",world"
是字面量。
And in your example, exclam
is a std::string
object.
在你的例子中,exclam
是一个std::string
对象。
C++ has an operator overload that takes a std::string
object and adds another string to it. When you concatenate a std::string
object with a literal it will make the appropriate casting for the literal.
C++ 有一个运算符重载,它接受一个std::string
对象并向其添加另一个字符串。当您将std::string
对象与文字连接时,它将为文字进行适当的转换。
But if you try to concatenate two literals, the compiler won't be able to find an operator that takes two literals.
但是,如果您尝试连接两个文字,编译器将无法找到接受两个文字的运算符。
回答by Juraj Blaho
Your second example does not work because there is no operator +
for two string literals. Note that a string literal is not of type string
, but instead is of type const char *
. Your second example will work if you revise it like this:
你的第二个例子不起作用,因为没有operator +
两个字符串文字。请注意,字符串文字不是 type string
,而是 type const char *
。如果您像这样修改它,您的第二个示例将起作用:
const string message = string("Hello") + ",world" + exclam;
回答by Thomas Sablik
Since C++14 you can use two real string literals:
从 C++14 开始,您可以使用两个真正的字符串文字:
const string hello = "Hello"s;
const string message = hello + ",world"s + "!"s;
or
或者
const string exclam = "!"s;
const string message = "Hello"s + ",world"s + exclam;
回答by Stephen
In case 1, because of order of operations you get:
在第 1 种情况下,由于操作顺序,您会得到:
(hello + ", world") + "!" which resolves to hello + "!" and finally to hello
(你好+“,世界”)+“!” 解析为 hello + "!" 最后问好
In case 2, as James noted, you get:
在情况 2 中,正如 James 所指出的,您会得到:
("Hello" + ", world") + exclam which is the concat of 2 string literals.
("Hello" + ", world") + exclam 这是 2 个字符串文字的连接。
Hope it's clear :)
希望它很清楚:)
回答by Péter T?r?k
The difference between a string (or to be precise, std::string
) and a character literal is that for the latter there is no +
operator defined. This is why the second example fails.
字符串(或者准确地说,std::string
)和字符文字之间的区别在于后者没有+
定义运算符。这就是第二个例子失败的原因。
In the first case, the compiler can find a suitable operator+
with the first argument being a string
and the second a character literal (const char*
) so it used that. The result of that operation is again a string
, so it repeats the same trick when adding "!"
to it.
在第一种情况下,编译器可以找到合适operator+
的第一个参数是 a string
,第二个参数是字符文字 ( const char*
) 所以它使用了它。该操作的结果再次是 a string
,因此在添加时重复相同的技巧"!"
。