std::endl vs ‘\n’1
The only difference is that
std::endl
flushes the output buffer, and'\n'
doesn’t. If you don’t want the buffer flushed frequently, use'\n'
.
两者唯一的不同在于,std::endl
刷新输出缓冲区,而\n
不。
std::cout << std::endl;
等价于
std::cout << '\n' << std::flush;
std::ostream 输出流的分类
std::cout <<:输出到控制台
std::ostringstream <<:输出到字符串流
std::ofstream <<:输出到文件流
流的输出以空格为分割
std::stringstream ss;
ss << "hello " << 5 << 5.5;
// "hello 55.5"
std::string s;
int a;
double d;
ss >> s >> a >> d;
// s: hello
// a: 55
// d: 0.50000...
ss.clear();
ss << "hello " << 5 << " " << 5.5;
// "hello 5 5.5"
ss >> s >> a >> d;
// s: hello
// a: 5
// d: 5.5
clear() 与 flush()
清空流中的数据是.clear()
成员,