C++基础::Stream(二)

来源:互联网 发布:南开大学 知乎 编辑:程序博客网 时间:2024/05/29 11:30

C++基础::Stream

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()成员,


  1. C++: “std::endl” vs “\n” ↩
0 0