Share via


basic_istream::operator>>

Calls a function on the input stream or reads formatted data from the input stream.

basic_istream& operator>>(
   basic_istream& (*_Pfn)(basic_istream&)
);
basic_istream& operator>>(
   ios_base& (*_Pfn)(ios_base&)
);
basic_istream& operator>>(
   basic_ios<Elem, Tr>& (*_Pfn)(basic_ios<Elem, Tr>&))
;
basic_istream& operator>>(
   basic_streambuf<Elem, Tr> *_Strbuf
);
basic_istream& operator>>(
   bool& _Val
);
basic_istream& operator>>(
   short& _Val
);
basic_istream& operator>>(
   unsigned short& _Val
);
basic_istream& operator>>(
   int& _Val
);
basic_istream& operator>>(
   unsigned int& _Val
);
basic_istream& operator>>(
   long& _Val
);
basic_istream& operator>>(
   unsigned long& _Val
);
basic_istream& operator>>(
   long long& _Val
);
basic_istream& operator>>(
   unsigned long long& _Val
);
basic_istream& operator>>(
   void *& _Val
);
basic_istream& operator>>(
   float& _Val
);
basic_istream& operator>>(
   double& _Val
);
basic_istream& operator>>(
   long double& _Val
);

Parameters

  • _Pfn
    A function pointer.

  • _Strbuf
    An object of type stream_buf.

  • _Val
    The value to read from the stream.

Return Value

The stream (*this).

Remarks

The <istream> header also defines several global extraction operators. For more information, see operator>> (<istream>).

The first member function ensures that an expression of the form istr >> ws calls ws(istr), and then returns *this. The second and third functions ensure that other manipulators, such as hex, behave similarly. The remaining functions constitute the formatted input functions.

The function:

basic_istream& operator>>(
    basic_streambuf<Elem, Tr> *_Strbuf);

extracts elements, if _Strbuf is not a null pointer, and inserts them in _Strbuf. Extraction stops on end of file. It also stops without extracting the element in question, if an insertion fails or throws an exception (which is caught but not rethrown). If the function extracts no elements, it calls setstate(failbit). In any case, the function returns *this.

The function:

basic_istream& operator>>(bool& _Val);

extracts a field and converts it to a Boolean value by calling use_facet <num_get<Elem, InIt>(getloc). get(InIt( rdbuf), Init(0), *this, getloc, _Val). Here, InIt is defined as istreambuf_iterator<Elem, Tr>. The function returns *this.

The functions:

basic_istream& operator>>(short& _Val);
basic_istream& operator>>(unsigned short& _Val);
basic_istream& operator>>(int& _Val);
basic_istream& operator>>(unsigned int& _Val);
basic_istream& operator>>(long& _Val);
basic_istream& operator>>(unsigned long& _Val);

basic_istream& operator>>(long long& _Val);
basic_istream& operator>>(unsigned long long& _Val);

basic_istream& operator>>(void *& _Val);

each extract a field and convert it to a numeric value by calling use_facet<num_get<Elem, InIt>(getloc). get(InIt( rdbuf), Init(0), *this, getloc, _Val). Here, InIt is defined as istreambuf_iterator<Elem, Tr>, and _Val has type long*,* unsigned long*,* or void * as needed.

If the converted value cannot be represented as the type of _Val, the function calls setstate(failbit). In any case, the function returns *this.

The functions:

basic_istream& operator>>(float& _Val);
basic_istream& operator>>(double& _Val);
basic_istream& operator>>(long double& _Val);

each extract a field and convert it to a numeric value by calling use_facet<num_get<Elem, InIt>(getloc). get(InIt( rdbuf), Init(0), *this, getloc, _Val). Here, InIt is defined as istreambuf_iterator<Elem, Tr>, and _Val has type double or long double as needed.

If the converted value cannot be represented as the type of _Val, the function calls setstate(failbit). In any case, it returns *this.

Example

// istream_basic_istream_op_is.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;

ios_base& hex2( ios_base& ib ) 
{
   ib.unsetf( ios_base::dec );
   ib.setf( ios_base::hex );
   return ib;
}

basic_istream<char, char_traits<char> >& somefunc(basic_istream<char, char_traits<char> > &i)
{
   if ( i == cin ) 
   {
      cerr << "i is cin" << endl;
   }
   return i;
}

int main( ) 
{
   int i = 0;
   cin >> somefunc;
   cin >> i;
   cout << i << endl;
   cin >> hex2;
   cin >> i;
   cout << i << endl;
}

Input

10
10

Sample Output

i is cin
10
10
10
16

Requirements

Header: <istream>

Namespace: std

See Also

Reference

basic_istream Class

operator>> (<istream>)

iostream Programming

iostreams Conventions