标准库标头 <istream>

来自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::istream std::basic_istream<char>
(typedef)
std::wistream std::basic_istream<wchar_t>
(typedef)
包装给定的抽象设备(std::basic_streambuf
并提供高层输入/输出接口
(类模板)
std::iostream std::basic_iostream<char>
(typedef)
std::wiostream std::basic_iostream<wchar_t>
(typedef)

函数

提取字符和字符数组
(函数模板)
操纵符
消耗空白符
(函数模板)

概要

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
    class basic_istream;
 
  using istream  = basic_istream<char>;
  using wistream = basic_istream<wchar_t>;
 
  template<class CharT, class Traits = char_traits<CharT>>
    class basic_iostream;
 
  using iostream  = basic_iostream<char>;
  using wiostream = basic_iostream<wchar_t>;
 
  template<class CharT, class Traits>
    basic_istream<CharT, Traits>& ws(basic_istream<CharT, Traits>& is);
 
  template<class Istream, class T>
    Istream&& operator>>(Istream&& is, T&& x);
}

类模板 std::basic_istream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_istream : 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_istream(basic_streambuf<CharT, Traits>* sb);
    virtual ~basic_istream();
 
    // 前置工序/后置工序
    class sentry;
 
    // 有格式输出
    basic_istream<CharT, Traits>&
      operator>>(basic_istream<CharT, Traits>& (*pf)(basic_istream<CharT, Traits>&));
    basic_istream<CharT, Traits>&
      operator>>(basic_ios<CharT, Traits>& (*pf)(basic_ios<CharT, Traits>&));
    basic_istream<CharT, Traits>&
      operator>>(ios_base& (*pf)(ios_base&));
 
    basic_istream<CharT, Traits>& operator>>(bool& n);
    basic_istream<CharT, Traits>& operator>>(short& n);
    basic_istream<CharT, Traits>& operator>>(unsigned short& n);
    basic_istream<CharT, Traits>& operator>>(int& n);
    basic_istream<CharT, Traits>& operator>>(unsigned int& n);
    basic_istream<CharT, Traits>& operator>>(long& n);
    basic_istream<CharT, Traits>& operator>>(unsigned long& n);
    basic_istream<CharT, Traits>& operator>>(long long& n);
    basic_istream<CharT, Traits>& operator>>(unsigned long long& n);
    basic_istream<CharT, Traits>& operator>>(float& f);
    basic_istream<CharT, Traits>& operator>>(double& f);
    basic_istream<CharT, Traits>& operator>>(long double& f);
 
    basic_istream<CharT, Traits>& operator>>(void*& p);
    basic_istream<CharT, Traits>& operator>>(basic_streambuf<char_type, Traits>* sb);
 
    // 无格式输出
    streamsize gcount() const;
    int_type get();
    basic_istream<CharT, Traits>& get(char_type& c);
    basic_istream<CharT, Traits>& get(char_type* s, streamsize n);
    basic_istream<CharT, Traits>& get(char_type* s, streamsize n, char_type delim);
    basic_istream<CharT, Traits>& get(basic_streambuf<char_type, Traits>& sb);
    basic_istream<CharT, Traits>&
      get(basic_streambuf<char_type, Traits>& sb, char_type delim);
 
    basic_istream<CharT, Traits>& getline(char_type* s, streamsize n);
    basic_istream<CharT, Traits>& getline(char_type* s, streamsize n, char_type delim);
 
    basic_istream<CharT, Traits>& ignore(streamsize n = 1, int_type delim = Traits::eof());
    int_type                      peek();
    basic_istream<CharT, Traits>& read    (char_type* s, streamsize n);
    streamsize                    readsome(char_type* s, streamsize n);
 
    basic_istream<CharT, Traits>& putback(char_type c);
    basic_istream<CharT, Traits>& unget();
    int sync();
 
    pos_type tellg();
    basic_istream<CharT, Traits>& seekg(pos_type);
    basic_istream<CharT, Traits>& seekg(off_type, ios_base::seekdir);
 
  protected:
    // 复制/移动构造函数
    basic_istream(const basic_istream&) = delete;
    basic_istream(basic_istream&& rhs);
 
    // 赋值与交换
    basic_istream& operator=(const basic_istream&) = delete;
    basic_istream& operator=(basic_istream&& rhs);
    void swap(basic_istream& rhs);
  };
 
  // 字符提取模板
  template<class CharT, class Traits>
    basic_istream<CharT, Traits>& operator>>(basic_istream<CharT, Traits>&, CharT&);
  template<class Traits>
    basic_istream<char, Traits>& operator>>(basic_istream<char, Traits>&, unsigned char&);
  template<class Traits>
    basic_istream<char, Traits>& operator>>(basic_istream<char, Traits>&, signed char&);
 
  template<class CharT, class Traits, size_t N>
    basic_istream<CharT, Traits>&
      operator>>(basic_istream<CharT, Traits>&, CharT(&)[N]);
  template<class Traits, size_t N>
    basic_istream<char, Traits>&
      operator>>(basic_istream<char, Traits>&, unsigned char(&)[N]);
  template<class Traits, size_t N>
    basic_istream<char, Traits>&
      operator>>(basic_istream<char, Traits>&, signed char(&)[N]);
}

std::basic_istream::sentry

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

类模板 std::basic_iostream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_iostream
    : public basic_istream<CharT, Traits>,
      public basic_ostream<CharT, Traits> {
  public:
    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_iostream(basic_streambuf<CharT, Traits>* sb);
 
    // 析构函数
    virtual ~basic_iostream();
 
  protected:
    // 构造函数
    basic_iostream(const basic_iostream&) = delete;
    basic_iostream(basic_iostream&& rhs);
 
    // 赋值与交换
    basic_iostream& operator=(const basic_iostream&) = delete;
    basic_iostream& operator=(basic_iostream&& rhs);
    void swap(basic_iostream& rhs);
  };
}