定义于头文件 <ostream>
| template<     class CharT, | 
类模板 basic_ostream 提供字符流上的高层输出操作。受支持操作包含有格式输出(例如整数值)和无格式输出(例如生字符和字符数组)。此功能以 basic_streambuf 类所提供的接口实现,通过 basic_ios 基类访问。典型的实现中, basic_ostream 无非继承的数据成员。

杂项
与底层存储设备同步
std::basic_ostream<CharT,Traits>::flush| basic_ostream& flush(); | 
写入未提交更改到底层输出序列。
若 rdbuf() 为空指针,则不做任何事。
否则,表现为无格式输出函数 (UnformattedOutputFunction) (C++11 起)。构造并检查 sentry 对象后,调用 rdbuf()->pubsync() 。若该调用返回 -1 ,则调用 setstate(badbit) 。
参数
(无)
返回值
*this
异常
若 exceptions()&badbit!=0 则可能抛出 std::ios_base::failure 。
调用示例
#include <thread>
#include <iostream>
#include <chrono>
void f()
{
    std::cout << "Output from thread...";
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "...thread calls flush()" << std::endl;
    std::cout.flush();
}
int main()
{
    std::thread t1(f);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::clog << "Output from main" << std::endl;
    t1.join();
}输出

交换流对象,除了关联缓冲区
std::basic_ostream<CharT,Traits>::swap| protected: | (C++11 起) | 
调用 basic_ios::swap(rhs) 交换 *this 和 rhs 间的基类数据成员,除了 rdbuf() 。此 swap 函数受保护:它为可交换输出流类 std::basic_ofstream 和 std::basic_ostringstream 的 swap 函数所调用,这些函数知晓如何正确交换关联的流缓冲。
参数
| rhs | - | 要与之交换的同类型 basic_ostream | 
调用示例
#include <sstream>
#include <iostream>
#include <utility>
int main()
{
    std::ostringstream s1("hello");
    std::ostringstream s2("bye");
    s1.swap(s2); // OK : ostringstream 拥有公开 swap()
    std::swap(s1, s2); // OK :调用 s1.swap(s2)
//  std::cout.swap(s2); // 错误: swap 是受保护成员
    std::cout << s1.str() << '\n';
}输出

成员类
为输出操作实现流准备的基本逻辑
std::basic_ostream<CharT,Traits>::sentry| class sentry; | 
类 basic_ostream::sentry 的对象在 std::basic_ostream 的每个进行输出(有格式与无格式)的成员函数起始的局部作用域中构造。其构造函数准备输出流:检查流是否已在失败状态,冲入所 tie() 的输出流,并且若需要则进行其他实现定义的任务。实现定义的清理,还有若有必要的输出流冲入,在析构函数中进行,从而保证若输出中抛出异常则得到执行。
成员函数
| (构造函数) | 构造 sentry 对象。所有准备任务都在此完成。 (公开成员函数) | 
| (析构函数) | 在有格式输入后或异常后终止化流对象,若有必要 (公开成员函数) | 
| operator= | 赋值运算符被删除 (公开成员函数) | 
| operator bool | 检查流对象的准备是否成功 (公开成员函数) | 
std::basic_ostream::sentry::sentry
| explicit sentry( std::basic_ostream<CharT,Traits>& os ); | 
为有格式输出准备流。
若 os.good() 为 false 则返回。否则若 os.tie() 不是空指针,则调用 os.tie()->flush() 以令输出序列同步于外部流。在准备中,构造函数可能调用 setstate(failbit) (它可能抛出 std::ios_base::failure )。
若在准备完成后 os.good() == true ,则任何对 operator bool 的后继调用都会返回 true 。
参数
| os | - | 要准备的输出流 | 
异常
若文件尾条件出现则为 std::ios_base::failure。
std::basic_ostream::sentry::~sentry
| ~sentry(); | 
若 (os.flags() & std::ios_base::unitbuf) && !std::uncaught_exception() && os.good()) 为 true ,则调用 os.rdbuf()->pubsync() 。若该函数返回 -1 ,则于 os.rdstate() 中设置 badbit ,而不传播异常。
std::basic_ostream::sentry::operator bool
| explicit operator bool() const; | 
检查输出流的准备是否成功。
参数
(无)
返回值
若输出流的准备成功则为 true ,否则为 false 。
调用示例
#include <iostream>
#include <sstream>
struct Foo
{
    char n[6];
};
std::ostream& operator<<(std::ostream& os, Foo& f)
{
    std::ostream::sentry s(os);
    if (s)
    {
        os.write(f.n, 5);
    }
    return os;
}
int main()
{
    Foo f = { "abcde" };
    std::cout << f << '\n';
    return 0;
}输出




















