定义于头文件 <istream>
| template<     class CharT, | 
 类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的输入(例如整数值或空白符分隔的字符与字符串)和无格式输入(例如未处理字符和字符数组)。此功能以通过 basic_ios 基类访问的底层 basic_streambuf 类所提供的接口实现。大多数库实现中, basic_istream 有一个非继承数据成员:用于存储 basic_istream::gcount() 所返回的值。
非成员函数
提取字符和字符数组
operator>>(std::basic_istream)| template< class CharT, class Traits > basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT& ch ); | (1) | |||
| template< class CharT, class Traits> basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT* s ); | (2) | (3) | (C++20 前) | |
| template< class CharT, class Traits, std::size_t N > basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT (&s)[N] ); | (C++20 起) | |||
| template< class CharT, class Traits, class T > | (C++11 起) (C++17 前) | |||
| template< class CharT, class Traits, class T > | (C++17 起) | 
1-2) 进行字符输入操作。
1) 表现为有格式输入函数 (FormattedInputFunction) 。在构造并检查 sentry 对象,因而可能跳过前导空白符后,释出一个字符并存储之到 ch。若无字符可用,则设置 failbit (在有格式输入函数 (FormattedInputFunction) 所要求的设置 eofbit 外)。
2) 表现为有格式输入函数 (FormattedInputFunction) 。在构造并检查 sentry 对象,因而可能跳过前导空白后,释出相继字符,并存储它们到 s 指向其首元素的字符数组 (C++20 前)的相继位置。若满足下列条件之一,则释出停止:
- 找到空白字符(以 ctype<CharT> 平面确定)。不释出该空白字符。
| 
 | (C++20 前) | 
| 
 | (C++20 起) | 
- 输入序列中出现文件尾(也会设置 eofbit )
任一情况下,存储额外的空字符值 CharT() 于输出结尾。若未释出字符,则设置 failbit (仍写入空字符到输出的首位置)。最后调用 st.width(0) 取消 std::setw 的效果,若存在。
3)
| 给定到输入流对象的右值引用,调用适合的释出运算符(等价于 st >> value )。 | (C++11 起) (C++17 前) | 
| 给定到输入流对象的右值引用,调用适合的释出运算符(等价于 st >> std::forward<T>(value) )。此重载仅若表达式 st >> std::forward<T>(value) 为良式才参与重载决议。 | (C++17 起) | 
注意
释出作为流的最末字符的单个字符不会设置 eofbit :这异于有格式输入函数,例如以 operator>> 释出最末的整数,但此行为匹配 std::scanf 用 "%c" 格式指定符的行为。
参数
| st | - | 要自之释出数据的输入流 | 
| ch | - | 到要存储释出字符的字符的引用 | 
| s | - | 指向 (C++20 前)要存储释出字符的字符数组的指针 | 
返回值
st
调用示例
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
    std::string input = "n greetings";
    std::istringstream stream(input);
    char c;
    const int MAX = 6;
    char cstr[MAX];
    stream >> c >> std::setw(MAX) >> cstr;
    std::cout << "c = " << c << '\n'
              << "cstr = " << cstr << '\n';
    double f;
    std::istringstream("1.23") >> f; // 右值流释出
    std::cout << "f = " << f << '\n';
    return 0;
}输出



















