std::basic_istream<CharT,Traits>::operator>>
来自 cppreference.cn
< cpp | io | basic istream
basic_istream& operator>>( unsigned short& value ); |
(1) | |
basic_istream& operator>>( unsigned int& value ); |
(2) | |
basic_istream& operator>>( long& value ); |
(3) | |
basic_istream& operator>>( unsigned long& value ); |
(4) | |
basic_istream& operator>>( long long& value ); |
(5) | (C++11 起) |
basic_istream& operator>>( unsigned long long& value ); |
(6) | (C++11 起) |
basic_istream& operator>>( float& value ); |
(7) | |
basic_istream& operator>>( double& value ); |
(8) | |
basic_istream& operator>>( long double& value ); |
(9) | |
basic_istream& operator>>( bool& value ); |
(10) | |
basic_istream& operator>>( void*& value ); |
(11) | |
basic_istream& operator>>( short& value ); |
(12) | |
basic_istream& operator>>( int& value ); |
(13) | |
basic_istream& operator>>( /* extended-floating-point-type */& value ); |
(14) | (C++23 起) |
basic_istream& operator>>( std::ios_base& (*func)(std::ios_base&) ); |
(15) | |
basic_istream& operator>>( std::basic_ios<CharT, Traits>& (*func)(std::basic_ios<CharT, Traits>&) ); |
(16) | |
basic_istream& operator>>( basic_istream& (*func)(basic_istream&) ); |
(17) | |
basic_istream& operator>>( std::basic_streambuf<CharT, Traits>* sb ); |
(18) | |
从输入流中提取值。
1-11) 提取一个值,可能跳过前面的空白字符。该值存储到给定的引用 value 中。
此函数表现为 FormattedInputFunction。在构造并检查岗哨对象(可能会跳过开头的空白字符)之后,通过调用 std::num_get::get() 提取一个值。
12) 提取一个 short 值,可能跳过前面的空白字符。该值存储到给定的引用 value 中。
此函数表现为 FormattedInputFunction。在构造并检查岗哨对象(可能会跳过开头的空白字符)之后,通过调用 std::num_get::get() 提取一个 long 值 lval。之后
- 如果 lval < std::numeric_limits<short>::min(),则设置
failbit
并将 std::numeric_limits<short>::min() 存储到 val 中。 - 否则,如果 std::numeric_limits<short>::max() < lval,则设置
failbit
并将 std::numeric_limits<short>::max() 存储到 val 中。 - 否则,将 static_cast<short>(lval) 存储到 val 中。
13) 提取一个 int 值,可能跳过前面的空白字符。该值存储到给定的引用 value 中。
此函数表现为 FormattedInputFunction。在构造并检查岗哨对象(可能会跳过开头的空白字符)之后,通过调用 std::num_get::get() 提取一个 long 值 lval。之后
- 如果 lval < std::numeric_limits<int>::min(),则设置
failbit
并将 std::numeric_limits<int>::min() 存储到 val 中。 - 否则,如果 std::numeric_limits<int>::max() < lval,则设置
failbit
并将 std::numeric_limits<int>::max() 存储到 val 中。 - 否则,将 static_cast<int>(lval) 存储到 val 中。
确定标准浮点类型
FP
如下:- 如果 /* extended-floating-point-type */ 的 浮点转换等级 小于或等于 float 的等级,则
FP
为 float。 - 否则,如果 /* extended-floating-point-type */ 的浮点转换等级小于或等于 double 的等级,则
FP
为 double。 - 否则,
FP
为 long double。
此函数表现为 FormattedInputFunction。在构造并检查岗哨对象(可能会跳过开头的空白字符)之后,通过调用 std::num_get::get() 提取一个
FP
值 fval。之后- 如果 fval < -std::numeric_limits</* extended-floating-point-type */>::max(),则设置
failbit
并将 -std::numeric_limits</* extended-floating-point-type */>::max() 存储到 val 中。 - 否则,如果 std::numeric_limits</* extended-floating-point-type */>::max() < fval,则设置
failbit
并将 std::numeric_limits</* extended-floating-point-type */>::max() 存储到 val 中。 - 否则,将 static_cast</* extended-floating-point-type */>(fval) 存储到 val 中。
15-17) 调用 func(*this),其中 func 是一个 I/O 操纵符。
18) 表现为 UnformattedInputFunction。在构造并检查岗哨对象之后,从 *this 提取所有数据并将其存储到 sb 中。当满足以下条件之一时,提取停止:
- 输入序列中出现文件结束;
- 插入到输出序列中失败(在这种情况下,要插入的字符不会被提取);
- 发生异常(在这种情况下,异常被捕获,并且只有在未插入任何字符且
exceptions()
中启用了failbit
时才重新抛出)。
如果提取失败(例如,如果在预期数字的地方输入了字母),则会将零写入 value 并设置 failbit
。对于有符号整数,如果提取的值太大或太小,无法放入 value 中,则会写入 std::numeric_limits<T>::max() 或 std::numeric_limits<T>::min()(分别)并设置 failbit
标志。对于无符号整数,如果提取的值太大或太小,无法放入 value 中,则会写入 std::numeric_limits<T>::max() 并设置 failbit
标志。
目录 |
[编辑] 参数
value | - | 对整数或浮点值的引用,用于存储提取的值 |
func | - | 指向 I/O 操纵符函数的指针 |
sb | - | 指向流缓冲区的指针,所有数据都将写入其中 |
[编辑] 返回值
1-16,18) *this
17) func(*this)
[编辑] 注意
对于重载 (14),当扩展浮点类型的浮点转换等级与任何标准浮点类型的等级不相等时,转换期间的双重舍入可能导致不准确的结果。std::from_chars() 可用于需要最大精度的情况。
[编辑] 示例
运行此代码
#include <iomanip> #include <iostream> #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'; // extract the rest using the streambuf overload stream >> std::cout.rdbuf(); std::cout << '\n'; }
输出
n = 41 f = 3.14 b = false hello world
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 64 | C++98 | 不清楚重载 (18) 是否只能重新抛出 调用 setstate(failbit) 抛出的 std::ios_base::failure |
所有捕获的异常 都可以重新抛出 |
LWG 118 | C++98 | 重载 (12,13) 将提取委托给 num_get::get, 但它没有 short 和 int 的重载 |
提取一个 long 值 而不是 short 或 int |
LWG 413 | C++98 | 重载 (18) 只重新抛出在从 sb 提取字符时抛出的异常 但是字符是从 *this 提取的 |
将 sb 更正为 *this |
LWG 567 | C++98 | 重载 (18) 表现为 FormattedInputFunction 由于 LWG issue 60 的决议 |
它表现为 UnformattedInputFunction(非格式化输入函数) |
LWG 661 | C++98 | 重载 (12,13) 未将提取的数字存储到 value 中,由于 LWG issue 118 的解决 |
如果未发生溢出,则存储该数字 未发生溢出 |
LWG 696 | C++98 | 在提取失败时,value 未改变 | 设置为零或最小值/ 最大值 |
[编辑] 另请参阅
提取字符和字符数组 (函数模板) | |
对字符串执行流输入和输出 (函数模板) | |
执行位集的流输入和输出 (函数模板) | |
序列化和反序列化复数 (函数模板) | |
(C++11) |
对伪随机数引擎执行流输入和输出 (函数模板) |
(C++11) |
对伪随机数分布执行流输入和输出 (函数模板) |
提取字符块 (公共成员函数) | |
提取已有的字符块 (公共成员函数) | |
提取字符 (公共成员函数) | |
提取字符直到找到给定字符 (公共成员函数) | |
(C++17) |
将字符序列转换为整数或浮点值 (函数) |