std::basic_istream<CharT,Traits>::operator>>
From 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) | (since 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) | |
Extracts values from an input stream.
1-4) Extracts an integer value potentially skipping preceding whitespace. The value is stored to a given reference
value
. This function behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts an integer value by calling std::num_get::get().
5) Extracts a floating-point value potentially skipping preceding whitespace. The value is stored to a given reference
value
. This function behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts a floating-point value by calling std::num_get::get().
6) Extracts a boolean value potentially skipping preceding whitespace. The value is stored to a given reference
value
. This function behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts an bool value by calling std::num_get::get().
7) Extracts a generic pointer value potentially skipping preceding whitespace. The value is stored to a given reference
value
. This function behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts a generic pointer value by calling std::num_get::get().
8-10) Calls func(*this), where
func
is an I/O manipulator.11) Behaves as an UnformattedInputFunction. After constructing and checking the sentry object, extracts all data from the input stream 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
failbit
is enabled inexceptions()
).
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 streambuffer to write all the data to |
Return value
1-9,11) *this
10) func(*this)
Example
Run this code
#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'; // 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 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) |