Namespaces
Variants
Views
Actions

std::basic_istream::operator>>

From cppreference.com
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 );

basic_istream& operator>>( long double& value );
(5)
basic_istream& operator>>( bool& value );
(6)
basic_istream& operator>>( void*& value );
(7)
basic_istream& operator>>( basic_istream& st,

                           std::ios_base& (*func)(std::ios_base&) );
basic_istream& operator>>( basic_istream& st,
                           std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_istream& operator>>( basic_istream& st,

                           std::basic_istream& (*func)(std::basic_istream&) );
(8)
basic_istream& operator>>( basic_istream& st,
                           std::basic_streambuf<CharT,Traits>* sb );
(9)

1-4) Extracts an integer value by calling num_get::get()

5) Extracts a floating point value by calling num_get::get()

6) Extracts bool value by calling num_get::get()

7) Extracts a generic pointer value by calling num_get::get()

8) Calls func(*this);, where func is an I/O manipulator.

9) Extracts all data 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).

In either case, stores the number of characters extracted in the member variable accessed by subsequent calls to gcount()

Contents

[edit] Notes

The (1-7) versions of the operator behave as formatted input functions. That is, they construct a sentry object at the beginning that flushes the tie()'d buffers if needed, checks for errors, and extracts and discards all leading whitespace characters unless the ios_base::skipws flag was cleared. The input is attempted only if the sentry object returns true.

The version 8) does not construct the sentry object. The version 9) constructs a sentry object with noskipws set to true.

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set (until C++11)

If extraction fails, zero is written to value and failbit is set. 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() is written and failbit flag is set. (since C++11)

[edit] 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

[edit] Return value

*this

[edit] Example

#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

[edit] See also

extracts characters and character arrays
(function template) [edit]
performs stream I/O of strings
(function template)
performs stream input and output of bitsets
(function)
serializes and deserializes a complex number
(function template)
performs stream input and output on pseudo-random number engine
(function) [edit]
performs stream input and output on pseudo-random number distribution
(function) [edit]
extracts blocks of characters
(public member function) [edit]
extracts already available blocks of characters
(public member function) [edit]
extracts characters
(public member function) [edit]
extracts characters until the given character is found
(public member function) [edit]