源代码
#include <iostream>
#include <string>
using namespace std;
int main(){
std::string s{'a', 'b', '\0', 'c'};
std::string s1="ab\0c";
cout<<s.size()<<" "<<s<<endl;
cout<<s1.size()<<" "<<s1<<endl;
return 0;
}
输出结果为:
4 abc
2 ab
我想知道为什么会发生这种现象,以及在C++中这两种初始化方式之间是否有区别?谢谢。
endl
。只需使用"\n"
即可。当写入终端控制台时,它通常会进行行缓冲,而当写入其他管道时,您不必每次都刷新缓冲区,从而避免了不必要的开销。 - wich