string变量到底是不是以\0结尾?
string a = "abc";
string b = "\0\0\0";
if (a[3] == '\0')
cout << "yes" << endl;
else
cout << "no" << endl;
于是又尝试一下代码:
string a = "abc";
string b = "\0\0\0";
cout << a.size() << endl;
cout << b.size() << endl;
输出是:
3
0
(以上代码均在VS2010里实现)这说明string变量的确是以'\0'来判断是否是string的结束。在C++11标准里,string已经是以'\0'作为结尾了。但是在C++99里,我猜想程序只是无法显式访问'\0'那一位,但是在stl内部实现仍然是以'\0'作为结尾。
如哪位网友有不同意见,欢迎讨论!