std::basic_istream<CharT,Traits>::operator>>
来自cppreference.com
                    
                                        
                    < cpp | io | basic istream
                    
                                                            
                    | basic_istream& operator>>( short& value ); basic_istream& operator>>( unsigned short& value ); | (1) | |
| basic_istream& operator>>( int& value ); basic_istream& operator>>( unsigned int& value ); | (2) | |
| basic_istream& operator>>( long& value ); basic_istream& operator>>( unsigned long& value ); | (3) | |
| basic_istream& operator>>( long long& value ); basic_istream& operator>>( unsigned long long& value ); | (4) | (C++11 起) | 
| basic_istream& operator>>( float& value ); basic_istream& operator>>( double& value ); | (5) | |
| basic_istream& operator>>( bool& value ); | (6) | |
| basic_istream& operator>>( void*& value ); | (7) | |
| basic_istream& operator>>( std::ios_base& (*func)(std::ios_base&) ); | (8) | |
| basic_istream& operator>>( std::basic_ios<CharT,Traits>&  (*func)(std::basic_ios<CharT,Traits>&) ); | (9) | |
| basic_istream& operator>>( basic_istream& (*func)(basic_istream&) ); | (10) | |
| basic_istream& operator>>( std::basic_streambuf<CharT,Traits>* sb ); | (11) | |
从输入流提取值。
1-4) 提取整数值,潜在地跳过前导空格。存储值到给定的引用 
value 。 此函数表现为有格式输入函数 (FormattedInputFunction) 。构造并检查 sentry 对象,可能跳过前导空格后,通过调用 std::num_get::get() 提取整数值。
5) 提取浮点值,潜在地跳过前导空格。存储值到给定的引用 
value 。 此函数表现为有格式输入函数 (FormattedInputFunction) 。构造并检查 sentry 对象,可能跳过前导空格后,通过调用 std::num_get::get() 提取浮点值。
6) 提取布尔值,潜在地跳过前导空格。存储值到给定的引用 
value 。 此函数表现为有格式输入函数 (FormattedInputFunction) 。构造并检查 sentry 对象,可能跳过前导空格后,通过调用 std::num_get::get() 提取 bool 值。
7) 提取泛用指针值,潜在地跳过前导空格。存储值到给定的引用 
value 。 此函数表现为有格式输入函数 (FormattedInputFunction) 。构造并检查 sentry 对象,可能跳过前导空格后,通过调用 std::num_get::get() 提取泛用指针值。
8-10) 调用 func(*this) ,其中 
func 为 I/O 操纵符。11) 表现为无格式输入函数 (UnformattedInputFunction) 。在构造并检查 sentry 对象后,从输入流提取所有数据并将它存储到 
sb 。若满足下列条件之一则提取停止:
- 输入序列上出现文件尾;
- 输出序列中插入失败(该情况下不提取要被插入的字符);
-  出现异常(该情况下捕捉异常,而仅若 exceptions()中启用了failbit才重抛)。
 
sb 为空指针或未插入字符到 sb 中,则调用 setstate(failbit) (若启用则会抛出 std::ios_base::failure )。若提取失败,则写入零到 value 并设置 failbit 。若提取结果对于 value 过大或过小,则写入 std::numeric_limits<T>::max() 或 std::numeric_limits<T>::min() 并设置 failbit 标志。
参数
| value | - | 到要存储提取值到的整数或浮点值的引用 | 
| func | - | 指向 I/O 操纵符函数的指针 | 
| sb | - | 指向要写入全部数据到的流缓冲的指针 | 
返回值
1-9,11) *this
10) func(*this)
示例
运行此代码
#include <iostream> #include <iomanip> #include <sstream> int main() { std::string input = "41 3.14 false hello world"; std::istringstream stream(input); int n; double f; bool b; stream >> n >> f >> std::boolalpha >> b; std::cout << "n = " << n << '\n' << "f = " << f << '\n' << "b = " << std::boolalpha << b << '\n'; // 用 streambuf 重载提取剩余内容 stream >> std::cout.rdbuf(); std::cout << '\n'; }
输出:
n = 41 f = 3.14 b = false hello world
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| DR | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 696 | C++98 | 提取失败时不更改 value | 设为零或最大/最小值 | 
参阅
| 提取字符和字符数组 (函数模板) | |
| 执行字符串的流输入与输出 (函数模板) | |
| 执行 bitset 的流输入和输出 (函数) | |
| 复数的序列化和反序列化 (函数模板) | |
| (C++11) | 执行伪随机数引擎的流输入和输出 (函数) | 
| (C++11) | 执行伪随机数分布的流输入和输出 (函数模板) | 
| 读并取走一块字符 (公开成员函数) | |
| 读并取走已经可用的字符块 (公开成员函数) | |
| 从流中读并取走(移除类似指针向下一个元素移动)一个字符 (公开成员函数) | |
| 一直读并取走字符,直至找到给定字符 (公开成员函数) | |
| (C++17) | 转换字符序列到整数或浮点值 (函数) |