在字符串中使用空字符 (C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11582365/
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
Use of null character in strings (C++)
提问by ewok
I am brushing up on my C++ and stumbled across a curious behavior in regards to strings, character arrays, and the null character ('\0'
). The following code:
我正在复习我的 C++ 并偶然发现了一个关于字符串、字符数组和空字符 ( '\0'
)的奇怪行为。以下代码:
#include <iostream>
using namespace std;
int main() {
cout << "hello> t
> t
>
there"[6] << endl;
char word [] = "hellochar word [] = "hellocout << &(word[6]) (char*, should print "there")
cout << &(word2[6]) (char* as well, undefined behaviour pre-C++11)
there";//Array of char...
cout << word[6] << endl; //So word[6] is the char't' (NOT a string)
string word2 = "hello##代码##there"; //std::string...
cout << word2[6] << endl; //so word2[6] is the char 't' (NOT a string as well)
there";
cout << word[6] << endl;
string word2 = "hello##代码##there";
cout << word2[6] << endl;
return 0;
}
produces the output:
产生输出:
##代码##What is going on behind the scenes? Why does the string literal and the declared char array store the 't'
at index 6 (after the internal '\0'
), but the declared string does not?
幕后发生了什么?为什么字符串文字和声明的 char 数组存储't'
在索引 6(在 internal 之后'\0'
),但声明的字符串没有?
采纳答案by sean
From what I remember, the first two are in essence just an array and the way a string is printed is to continue to print until a \0
is encounterd. Thus in the first two examples you start at the point offset of the 6th character in the string, but in your case you are printing out the 6th character which is t
.
据我所知,前两个本质上只是一个数组,打印字符串的方式是继续打印直到\0
遇到 a 。因此,在前两个示例中,您从字符串中第 6 个字符的点偏移量开始,但在您的情况下,您打印的是第 6 个字符,即t
.
What happens with the string
class is that it makes a copy of the string into it's own internal buffer and does so by copying the string from the start of the array up to the first \0
it finds. Thus the t
is not stored because it comes after the first \0
.
string
类发生的事情是它将字符串的副本复制到它自己的内部缓冲区中,并通过将字符串从数组的开头复制到\0
它找到的第一个来做到这一点。因此t
不会被存储,因为它在第一个之后\0
。
回答by Oliver Charlesworth
Because the std::string
constructor that takes a const char*
treats its argument as a C-style string. It simply copies from it until it hits a null-terminator, then stops copying.
因为std::string
采用 a的构造函数const char*
将其参数视为 C 样式字符串。它只是从中复制,直到遇到空终止符,然后停止复制。
So your last example is actually invoking undefined behaviour; word2[6]
goes past the end of the string.
所以你的最后一个例子实际上是在调用未定义的行为;word2[6]
越过字符串的末尾。
回答by RedX
You are constructing a string from a char*
(or something that decayed to that). This means that the convention for C-strings apply. That is they are '\0'
terminated. That's why word2
only contains "hello"
.
您正在从 a char*
(或衰减到那个的东西)构造一个字符串。这意味着 C 字符串的约定适用。那就是他们被'\0'
终止了。这就是为什么word2
只包含"hello"
.
回答by Gerasimos R
The problem is that you are not printing strings at all - you are printing single characters.
问题是您根本没有打印字符串 - 您正在打印单个字符。
##代码##So, you are invoking the "char" overloads, not the "char*" or "string" overloads at all, and the NULL chars have nothing to do with it at all : You are just printing the 6th character of word, and the 6th character of word2.
因此,您正在调用“char”重载,而不是“char*”或“string”重载,而 NULL 字符与它完全无关:您只是在打印 word 的第 6 个字符,而word2 的第 6 个字符。
If I am reading your intent correctly, your test should read:
如果我正确阅读了您的意图,则您的测试应为:
##代码##In C++11 and later this will also print "there" And be well defined
在 C++11 及更高版本中,这也将打印“那里”并且定义良好