标准库标头 <ostream>

来自cppreference.com
< cpp‎ | header


 
 
标准库头
通用工具
<any> (C++17)
<bitset>
<bit> (C++20)
<charconv> (C++17)
<expected> (C++23)
<format> (C++20)
<functional>
<optional> (C++17)
<stdbit.h> (C++26)
<tuple> (C++11)
<typeindex> (C++11)
<utility>
<variant> (C++17)
容器
<array> (C++11)
<deque>
<flat_map> (C++23)
<flat_set> (C++23)
<forward_list> (C++11)
<hive> (C++26)
<inplace_vector> (C++26)   
<list>
<map>
<mdspan> (C++23)
<queue>
<set>
<span> (C++20)
<stack>
<unordered_map> (C++11)
<unordered_set> (C++11)
<vector>
迭代器
<iterator>
范围
<generator> (C++23)
<ranges> (C++20)
 

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

目录

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

函数

插入字符数据,或向右值流插入
(函数) [编辑]
输出各实参的格式化表示
(函数模板) [编辑]
输出各实参的格式化表示并追加 '\n'
(函数模板) [编辑]
使用类型擦除进行各实参的格式化表示的知 Unicode 输出
(函数) [编辑]
使用类型擦除输出各实参的格式化表示
(函数) [编辑]
操纵符
输出 '\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);
 
  // 打印函数
  template<class... Args>
    void print(ostream& os, format_string<Args...> fmt, Args&&... args);
  template<class... Args>
    void println(ostream& os, format_string<Args...> fmt, Args&&... args);
  void println(ostream& os);
 
  void vprint_unicode(ostream& os, string_view fmt, format_args args);
  void vprint_nonunicode(ostream& os, string_view fmt, format_args args);
}

[编辑] 类模板 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& operator<<(basic_ostream& (*pf)(basic_ostream&));
    basic_ostream& operator<<(basic_ios<CharT, Traits>& (*pf)(basic_ios<CharT, Traits>&));
    basic_ostream& operator<<(ios_base& (*pf)(ios_base&));
 
    basic_ostream& operator<<(bool n);
    basic_ostream& operator<<(short n);
    basic_ostream& operator<<(unsigned short n);
    basic_ostream& operator<<(int n);
    basic_ostream& operator<<(unsigned int n);
    basic_ostream& operator<<(long n);
    basic_ostream& operator<<(unsigned long n);
    basic_ostream& operator<<(long long n);
    basic_ostream& operator<<(unsigned long long n);
    basic_ostream& operator<<(float f);
    basic_ostream& operator<<(double f);
    basic_ostream& operator<<(long double f);
    basic_ostream& operator<<(/*extended-floating-point-type*/ f);
 
    basic_ostream& operator<<(const void* p);
    basic_ostream& operator<<(const volatile void* p);
    basic_ostream& operator<<(nullptr_t);
    basic_ostream& operator<<(basic_streambuf<char_type, Traits>* sb);
 
    // 无格式输出
    basic_ostream& put(char_type c);
    basic_ostream& write(const char_type* s, streamsize n);
 
    basic_ostream& flush();
 
    // 巡位
    pos_type tellp();
    basic_ostream& seekp(pos_type);
    basic_ostream& 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>
  class basic_ostream<CharT, Traits>::sentry {
    bool ok_;       // 仅用于阐释
  public:
    explicit sentry(basic_ostream& os);
    ~sentry();
    explicit operator bool() const { return ok_; }
 
    sentry(const sentry&) = delete;
    sentry& operator=(const sentry&) = delete;
  };
}