'\0'是什么意思? [英] What does '\0' mean?

查看:2756
本文介绍了'\0'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我不能理解两个不同位置的\0是什么意思:

  string x =hhhdef\\\
;
cout<< x<< endl
x [3] ='\0';
cout<< x<< endl
cout<<hhh\0defef\\\
<< endl;

结果:


hhhdef



hhhef



hhh


$ b

解决方案

C ++ std:
$ b

任何人都可以给我一些指针? :string s是计数字符串 - 即,它们的长度存储为整数,并且它们可以包含任何字符。当你用一个 \0 替换第三个字符时,没有什么特别的 - 它被打印,就像它是任何其他字符(特别是,你的控制台只是忽略它) p>

在最后一行,打印一个C字符串,其结尾由第一个 \0 找到。在这种情况下, cout 继续打印字符,直到找到 \0 ,在您的情况下,是在第三个 h 之后。


I can't understand what the '\0' in the two different place mean in the following code:

string x = "hhhdef\n";
cout << x << endl;
x[3]='\0';
cout << x << endl;
cout<<"hhh\0defef\n"<<endl;

Result:

hhhdef

hhhef

hhh

Can anyone give me some pointers?

解决方案

C++ std::strings are "counted" strings - i.e., their length is stored as an integer, and they can contain any character. When you replace the third character with a \0 nothing special happens - it's printed as if it was any other character (in particular, your console simply ignores it).

In the last line, instead, you are printing a C string, whose end is determined by the first \0 that is found. In such a case, cout goes on printing characters until it finds a \0, which, in your case, is after the third h.

这篇关于'\0'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆