标准库标头 <ostream>

来自cppreference.com
< cpp‎ | header
 
 
标准库标头
注:修订记号中的反斜杠 '/' 意味着此标头被弃用和/或被移除。
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)
内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用工具
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<expected> (C++23)
<bitset>

<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)

迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<numbers> (C++20)

日期时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17)
输入/输出
<istream>
<ostream>
<iomanip>
<print> (C++23)
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/)
正则表达式
<regex>
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)
<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)
<barrier> (C++20)
<future> (C++11)

C 兼容
<cstdbool> (C++11/17/20)  
<ccomplex> (C++11/17/20)
<ctgmath> (C++11/17/20)

<cstdalign> (C++11/17/20)

<ciso646> (C++20 前)

 

此头文件是输入/输出库的一部分。

包装给定的抽象设备(std::basic_streambuf
并提供高层输出接口
(类模板)
std::ostream std::basic_ostream<char>
(typedef)
std::wostream std::basic_ostream<wchar_t>
(typedef)

函数

插入字符数据
(函数)
操纵符
输出 '\n' 并冲洗输出流
(函数模板)
输出 '\0'
(函数模板)
冲洗输出流
(函数模板)
控制流的 basic_syncbuf 是否在冲入时发射
(函数模板)
冲入流,而若它使用 basic_syncbuf 则发射其内容
(函数模板)

概要

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
    class basic_ostream;
 
  using ostream  = basic_ostream<char>;
  using wostream = basic_ostream<wchar_t>;
 
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& endl(basic_ostream<CharT, Traits>& os);
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& ends(basic_ostream<CharT, Traits>& os);
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& flush(basic_ostream<CharT, Traits>& os);
 
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& emit_on_flush(basic_ostream<CharT, Traits>& os);
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& noemit_on_flush(basic_ostream<CharT, Traits>& os);
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& flush_emit(basic_ostream<CharT, Traits>& os);
 
  template<class Ostream, class T>
    Ostream&& operator<<(Ostream&& os, const T& x);
}

类模板 std::basic_ostream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_ostream : virtual public basic_ios<CharT, Traits> {
  public:
    // 类型(继承自 basic_ios )
    using char_type   = CharT;
    using int_type    = typename Traits::int_type;
    using pos_type    = typename Traits::pos_type;
    using off_type    = typename Traits::off_type;
    using traits_type = Traits;
 
    // 构造函数/析构函数
    explicit basic_ostream(basic_streambuf<char_type, Traits>* sb);
    virtual ~basic_ostream();
 
    // 前置工序/后置工序
    class sentry;
 
    // 有格式输出
    basic_ostream<CharT, Traits>&
      operator<<(basic_ostream<CharT, Traits>& (*pf)(basic_ostream<CharT, Traits>&));
    basic_ostream<CharT, Traits>&
      operator<<(basic_ios<CharT, Traits>& (*pf)(basic_ios<CharT, Traits>&));
    basic_ostream<CharT, Traits>&
      operator<<(ios_base& (*pf)(ios_base&));
 
    basic_ostream<CharT, Traits>& operator<<(bool n);
    basic_ostream<CharT, Traits>& operator<<(short n);
    basic_ostream<CharT, Traits>& operator<<(unsigned short n);
    basic_ostream<CharT, Traits>& operator<<(int n);
    basic_ostream<CharT, Traits>& operator<<(unsigned int n);
    basic_ostream<CharT, Traits>& operator<<(long n);
    basic_ostream<CharT, Traits>& operator<<(unsigned long n);
    basic_ostream<CharT, Traits>& operator<<(long long n);
    basic_ostream<CharT, Traits>& operator<<(unsigned long long n);
    basic_ostream<CharT, Traits>& operator<<(float f);
    basic_ostream<CharT, Traits>& operator<<(double f);
    basic_ostream<CharT, Traits>& operator<<(long double f);
 
    basic_ostream<CharT, Traits>& operator<<(const void* p);
    basic_ostream<CharT, Traits>& operator<<(const volatile void* val);
    basic_ostream<CharT, Traits>& operator<<(nullptr_t);
    basic_ostream<CharT, Traits>& operator<<(basic_streambuf<char_type, Traits>* sb);
 
    // 无格式输出
    basic_ostream<CharT, Traits>& put(char_type c);
    basic_ostream<CharT, Traits>& write(const char_type* s, streamsize n);
 
    basic_ostream<CharT, Traits>& flush();
 
    // 巡位
    pos_type tellp();
    basic_ostream<CharT, Traits>& seekp(pos_type);
    basic_ostream<CharT, Traits>& seekp(off_type, ios_base::seekdir);
 
  protected:
    // 复制/移动构造函数
    basic_ostream(const basic_ostream&) = delete;
    basic_ostream(basic_ostream&& rhs);
 
    // 赋值与交换
    basic_ostream& operator=(const basic_ostream&) = delete;
    basic_ostream& operator=(basic_ostream&& rhs);
    void swap(basic_ostream& rhs);
  };
 
  // 字符插入器
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>&, CharT);
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>&, char);
  template<class Traits>
    basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>&, char);
 
  template<class Traits>
    basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>&, signed char);
  template<class Traits>
    basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>&, unsigned char);
 
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, wchar_t) = delete;
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, char8_t) = delete;
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, char16_t) = delete;
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, char32_t) = delete;
  template<class Traits>
    basic_ostream<wchar_t, Traits>&
      operator<<(basic_ostream<wchar_t, Traits>&, char8_t) = delete;
  template<class Traits>
    basic_ostream<wchar_t, Traits>&
      operator<<(basic_ostream<wchar_t, Traits>&, char16_t) = delete;
  template<class Traits>
    basic_ostream<wchar_t, Traits>&
      operator<<(basic_ostream<wchar_t, Traits>&, char32_t) = delete;
 
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>&
      operator<<(basic_ostream<CharT, Traits>&, const CharT*);
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>&
      operator<<(basic_ostream<CharT, Traits>&, const char*);
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, const char*);
 
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, const signed char*);
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, const unsigned char*);
 
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, const wchar_t*) = delete;
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, const char8_t*) = delete;
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, const char16_t*) = delete;
  template<class Traits>
    basic_ostream<char, Traits>&
      operator<<(basic_ostream<char, Traits>&, const char32_t*) = delete;
  template<class Traits>
    basic_ostream<wchar_t, Traits>&
      operator<<(basic_ostream<wchar_t, Traits>&, const char8_t*) = delete;
  template<class Traits>
    basic_ostream<wchar_t, Traits>&
      operator<<(basic_ostream<wchar_t, Traits>&, const char16_t*) = delete;
  template<class Traits>
    basic_ostream<wchar_t, Traits>&
      operator<<(basic_ostream<wchar_t, Traits>&, const char32_t*) = delete;
}

std::basic_ostream::sentry

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_ostream<CharT, Traits>::sentry {
    bool ok_;       // 仅用于阐释
  public:
    explicit sentry(basic_ostream<CharT, Traits>& os);
    ~sentry();
    explicit operator bool() const { return ok_; }
 
    sentry(const sentry&) = delete;
    sentry& operator=(const sentry&) = delete;
  };
}