Namespaces
Variants
Views
Actions

std::basic_istream::operator>>

From cppreference.com
< cpp‎ | io‎ | basic istream
Revision as of 21:48, 3 December 2012 by Cubbi (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( short& value );
basic_istream& operator>>( unsigned short& value );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( int& value );
basic_istream& operator>>( unsigned int& value );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( long& value );
basic_istream& operator>>( unsigned long& value );
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( long long& value );
basic_istream& operator>>( unsigned long long& value );
</td>

<td > (4) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( float& value );

basic_istream& operator>>( double& value );

basic_istream& operator>>( long double& value );
</td>

<td > (5) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( bool& value );
</td>

<td > (6) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( void*& value );
</td>

<td > (7) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
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&) );
</td>

<td > (8) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_istream& operator>>( basic_istream& st,
                           std::basic_streambuf<CharT,Traits>* sb );
</td>

<td > (9) </td> <td class="t-dcl-nopad"> </td> </tr>

Template:ddcl list end

1-4) Behaves as an FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts an integer value by calling num_get::get()

5) Behaves as an FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts a floating point value by calling num_get::get()

6) Behaves as an FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts bool value by calling num_get::get()

7) Behaves as an FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts a generic pointer value by calling num_get::get()

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

9) 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 exceptions on failbit are enabled).

In either case, stores the number of characters extracted in the member variable accessed by subsequent calls to Template:rlpf. If sb is a null pointer or if no characters are inserted into sb, sets eofbit.

Template:rev list begin Template:rev list item Template:rev list item Template:rev list end

Contents

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

*this

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

See also

Template:cpp/io/basic istream/dcl list operator gtgt2Template:cpp/numeric/random/engine/dcl list operator ltltgtgtTemplate:cpp/numeric/random/distribution/dcl list operator ltltgtgtTemplate:cpp/io/basic istream/dcl list readTemplate:cpp/io/basic istream/dcl list readsomeTemplate:cpp/io/basic istream/dcl list getTemplate:cpp/io/basic istream/dcl list getline
performs stream I/O of strings
(function template)
performs stream input and output of bitsets
(function)
serializes and deserializes a complex number
(function template)