连接两个字符串文字 [英] Concatenate two string literals
问题描述
我是编程的新手,我正在读Koenig的Accelerated C ++。无论如何,我正在学习字符串,他写道新的想法是,我们可以使用+来连接一个字符串和一个字符串字面量 - 或者说,两个字符串(但不是两个字符串字面量)。
I am very new to programming, and am reading Accelerated C++ by Koenig. Anyways, I am learning about strings and 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).
好,这是有道理的,现在分两个单独的练习意在照亮这个。
Fine, this makes sense I suppose. Now onto two separate exercises meant to illuminate this .
以下定义是否有效?
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.
谢谢!
推荐答案
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);
正如你所看到的,两个字符串文字Hello
和,世界
首先是添加,因此出错。
As you can see, the two string literals "Hello"
and ",world"
are "added" first, hence the error.
前两个串联的字符串必须是 std :: string
对象:
One of the first two strings being concatenated must be a std::string
object:
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);
你的第一个例子( hello +,world !
),因为 std :: string
( hello
)最左边的参数 +
。 +
被计算,结果是一个 std :: string
对象与连接的字符串, c $ c> std :: string 然后与!
连接。
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 "!"
.
至于为什么,您不能使用 +
连接两个字符串文字,一个字符串字面量只是一个字符数组( const char [N]
其中 N
加一,为null终止符)。当您在大多数上下文中使用数组时,它将转换为指向其初始元素的指针。
As for why you 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.
因此,当您尝试执行Hello+,world
做的是添加两个 const char *
s在一起,这是不可能的(什么意思是添加两个指针在一起?),如果它是不会做你想要什么。
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.
请注意,您可以通过将字符串文字放在彼此旁边来连接;例如,以下两个是等效的:
Note that you can concatenate string literals by placing them next to each other; for example, the following two are equivalent:
"Hello" ",world"
"Hello,world"
如果你有一个长字符串要分解成多行,这很有用。它们必须是字符串字面量,但是:这不会与 const char *
指针或 const char [N]
数组。
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.
这篇关于连接两个字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!