记录一下昨晚的一个问题,c语言中的字符串是以'\0'为结尾的标识符的,那c++的呢?
string s1 = "hello\0word";
cout << "s1的长度=" << s1.size() << endl;
cout << "s1=" << s1 << endl;
结果如下:
我们发现这好像是c的标准,以'\0'为标识符截断了,那能不能不把完整的字符串截断呢?经过查找资料,确实是可以的,c++有c++的标准,std::string_literals,使用很简单,字符串后面加上s。
string s1 = "hello\0world";
string s2 = "hello\0world"s;
if (s1 == s2)
{
cout << "ok" << endl;;
}
cout << "s1的长度=" << s1.size() << endl;
cout << "s2的长度=" << s2.size() << endl;
cout << "s1=" << s1 << endl;
cout << "s2=" << s2 << endl;
结果如下:
这是昨天作为一个冒牌老师给人讲课时发现的一个问题,今早特来总结记录,看来还是基础不牢固啊。路漫漫其修远兮,吾将上下而求索!