On this page
std::basic_istream<CharT,Traits>::operator>>
|
(1) | |
|
(2) | |
|
(3) | |
|
(4) | |
|
(5) | (since C++11) |
|
(6) | (since C++11) |
|
(7) | |
|
(8) | |
|
(9) | |
|
(10) | |
|
(11) | |
|
(12) | |
|
(13) | |
|
(14) | (since C++23) |
|
(15) | |
|
(16) | |
|
(17) | |
|
(18) |
Extracts values from an input stream.
value
.
std::num_get::get()
.
short
value potentially skipping preceding whitespace. The value is stored to a given reference value
.
lval
by calling std::num_get::get()
. After that:
- If
lval < std::numeric_limits<short>::min()
, setsfailbit
and storesstd::numeric_limits<short>::min()
toval
. - Otherwise, if
std::numeric_limits<short>::max() < lval
, setsfailbit
and storesstd::numeric_limits<short>::max()
toval
. - Otherwise, stores
static_cast<short>(lval)
toval
.
int
value potentially skipping preceding whitespace. The value is stored to a given reference value
.
lval
by calling std::num_get::get()
. After that:
- If
lval < std::numeric_limits<int>::min()
, setsfailbit
and storesstd::numeric_limits<int>::min()
toval
. - Otherwise, if
std::numeric_limits<int>::max() < lval
, setsfailbit
and storesstd::numeric_limits<int>::max()
toval
. - Otherwise, stores
static_cast<int>(lval)
toval
.
value
. The library provides overloads for all cv-unqualified extended floating-point types as the referenced type of the parameter value
.
FP
as follows:
- If the floating-point conversion rank of /* extended-floating-point-type */ is less than or equal to that of float, then
FP
is float. - Otherwise, if the floating-point conversion rank of /* extended-floating-point-type */ is less than or equal to that of double, then
FP
is double. - Otherwise,
FP
is long double.
FP
value fval
by calling std::num_get::get()
. After that:
- If
fval < -std::numeric_limits</* extended-floating-point-type */>::max()
, setsfailbit
and stores-std::numeric_limits</* extended-floating-point-type */>::max()
toval
. - Otherwise, if
std::numeric_limits</* extended-floating-point-type */>::max() < fval
, setsfailbit
and storesstd::numeric_limits</* extended-floating-point-type */>::max()
toval
. - Otherwise, stores
static_cast</* extended-floating-point-type */>(fval)
toval
.
func(*this)
, where func
is an I/O manipulator.
*this
and stores it to sb
. The extraction stops if one of the following conditions are met:
- end-of-file occurs on the input sequence;
- inserting in the output sequence fails (in which case the character to be inserted is not extracted);
- an exception occurs (in which case the exception is caught, and only rethrown if it inserted no characters and
failbit
is enabled inexceptions()
).
gcount()
. If sb
is a null pointer or if no characters were inserted into sb
, calls setstate(failbit)
(which may throw std::ios_base::failure
if enabled).
If extraction fails (e.g. if a letter was entered where a digit is expected), zero is written to value
and failbit
is set. For signed integers, if extraction results in the value too large or too small to fit in value
, std::numeric_limits<T>::max()
or std::numeric_limits<T>::min()
(respectively) is written and failbit
flag is set. For unsigned integers, if extraction results in the value too large or too small to fit in value
, std::numeric_limits<T>::max()
is written and failbit
flag is set.
Parameters
value | - | reference to an integer or floating-point value to store the extracted value to |
func | - | pointer to I/O manipulator function |
sb | - | pointer to the stream buffer to write all the data to |
Return value
*this
func(*this)
Notes
For overload (14), when the extended floating-point type has a floating-point conversion rank that is not equal to the rank of any standard floating-point type, then double rounding during the conversion can result in inaccurate results. std::from_chars()
can be used in situations where maximum accuracy is important.
Example
#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';
}
Output:
n = 41
f = 3.14
b = false
hello world
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 64 | C++98 | it was unclear whether overload (18) can only rethrow thestd::ios_base::failure thrown by calling setstate(failbit) |
all exceptions caught can be rethrown |
LWG 118 | C++98 | overload (12,13) delegated the extraction to num_get::get ,but it does not have overloads for short and int |
a long value is extracted instead of short or int |
LWG 413 | C++98 | overload (18) only rethrew exceptions thrown while extracting characters from sb , but characters are extracted from *this |
corrected sb to *this |
LWG 567 | C++98 | overload (18) behaved as a FormattedInputFunction because of the resolution of LWG issue 60 |
it behaves as an UnformattedInputFunction |
LWG 661 | C++98 | overloads (12,13) did not store the extracted number to value due to the resolution of LWG issue 118 |
stores the number if no overflow occurs |
LWG 696 | C++98 | value was unchanged on extraction failure |
set to zero or minimum/ maximum values |
See also
extracts characters and character arrays (function template) |
|
performs stream input and output on strings (function template) |
|
performs stream input and output of bitsets (function template) |
|
serializes and deserializes a complex number (function template) |
|
(C++11)
|
performs stream input and output on pseudo-random number engine (function template) |
(C++11)
|
performs stream input and output on pseudo-random number distribution (function template) |
extracts blocks of characters (public member function) |
|
extracts already available blocks of characters (public member function) |
|
extracts characters (public member function) |
|
extracts characters until the given character is found (public member function) |
|
(C++17)
|
converts a character sequence to an integer or floating-point value (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt