istream::operator>> istream
  cplusplus.com  

Declaration prototypes:

class members:
 
istream& operator>> (bool& val );
istream& operator>> (short& val );
istream& operator>> (unsigned short& val );
istream& operator>> (int& val );
istream& operator>> (unsigned int& val );
istream& operator>> (long& val );
istream& operator>> (unsigned long& val );
istream& operator>> (float& val );
istream& operator>> (double& val );
istream& operator>> (long double& val );
istream& operator>> (void*& val );
 
istream& operator>> (streambuf& sb );
 
istream& operator>> (istream& ( *pf )(istream&));
istream& operator>> (ios& ( *pf )(ios&));
istream& operator>> (ios_base& ( *pf )(ios_base&));
 
external operator>>  overloaded functions:
 
istream& operator>> (istream& is, char& ch );
istream& operator>> (istream& is, signed char& ch );
istream& operator>> (istream& is, unsigned char& ch );
 
istream& operator>> (istream& is, char* str );
istream& operator>> (istream& is, signed char* str );
istream& operator>> (istream& is, unsigned char* str );

Perform a formatted input operation (extraction).
  Extracts data from the stream and stores it in the parameter passed (right-value). The amount of data extracted and the way it is interpreted and stored depends on the type of parameter (see section parameters).
  There are two groups of functions shown above:

Class members
These are member functions of the class istream. They have only one parameter (the right-value), and may be called by any of the following ways (assuming istr is an object of class istream and x an object of one of the supported types):
  istr.operator>> (x);
  istr >> x;
External operator>> overloaded functions
These are not members of istream, they are global functions, but their description has been included here because of their similarity of use and complementariety with the members above. They may be called by any of the following ways:
  operator>> (istr,x);
  istr >> x;
In both cases the most used is the second one shown ( istr >> x ) and it is also the suggested way to avoid certain unwanted implicit conversions.

Parameters.

val
Extracts data from stream and tries to interpret it as a numeric value of the given parameter's type. If successful the value is stored in val. These extractions depend on the object's locale information to parse data.
sb
Extracts characters from stream and stores them in sb until the End-Of-File is reached or until the buffer fails to successfully insert a character (in which case this last character is not extracted).
pf
Calls pf(*this). This is intended to be used with manipulators (see ahead).
ch
A single character is extracted and stored in ch.
str
Extracts characters and stores them in succesive locations starting at location pointed by str. Extraction ends when the next element is either a valid whitespace or a null character, or if the End-Of-File is reached.
A null character is automatically appended after the extracted characters.
The extraction operation can be limited to a certain number of characters (thus avoiding the possibility of buffer overflow) if the field width inherited member (ios_base::width) is set to a value greater than 0. In this case, the extraction ends one character before the count of characters extracted reaches the value of field width leaving space for the ending null character. After a call to this extraction operation the value of the field width is reset to 0.
is
istream object on which the action is performed. This is a parameter of the external overloaded operator>> functions and means the left-value of the call to that operator.

Return Value.
  member functions return *this
  external functions return is

Manipulators.
  Certain global functions are specifically designed to be used as a parameter for this member function and modify internal flags of the stream that affect formatted input:

boolalphaconsider alphabetic sequences true and false as valid bool type values.
decinterpret numeric values in decimal format.
hexinterpret numeric values in hexadecimal format.
noboolalphado not consider alphabetic sequences true and false as valid bool type values.
noskipwsdo not skip possible white spaces before extractions.
octinterpret numeric values in octal format.
skipwsskip any possible white space before extractions.
wsdiscard sequence of whitespaces.

Manipulators fixed, internal, left, noshowbase, noshowpoint, noshowpos, nounitbuf, nouppercase, right, scientific, showbase, showpoint, showpos, unitbuf and uppercase may also be applied using operator>> member function, but have no effect on input operations.

If header file <iomanip> is included special manipulators resetiosflags, setbase, setfill, setiosflags, setprecision and setw may be used with this function.

Example.

// istream::operator>>
#include <iostream>
using namespace std;

int main () {
  int n;
  char str[10];

  cout << "Enter a number: ";
  cin >> n;
  cout << "You have entered: " << n << endl;

  cout << "Enter a hex number: ";
  cin >> hex;            // manipulator
  cin >> n;
  cout << "The decimal equivalent is: " << n << endl;

  cout << "Enter a word: ";
  cin.width (10);        // limit width
  cin >> str;
  cout << "The first 9 chars of your word are: " << str << endl;

  return 0;
}
  The source demonstrates the use of some of the overloaded operator>> functions shown above using the istream object cin

Basic template member declarations ( basic_istream<charT,traits> ):
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>> (float& val );
basic_istream& operator>> (double& val );
basic_istream& operator>> (long double& val );
basic_istream& operator>> (void*& val );
basic_istream& operator>> (basic_streambuf<charT,traits>& sb );
basic_istream& operator>> (basic_istream& ( *pf )(istream&));
basic_istream& operator>> (basic_ios<charT,traits>& ( *pf )(basic_ios<charT,traits>&));
basic_istream& operator>> (ios_base& ( *pf )(ios_base&));
External operator>>  overloaded functions:
template <class charT, class traits>
  basic_istream<charT,traits>& operator>> (basic_istream<charT,traits>& is, charT& ch );

template <class charT, class traits>
  basic_istream<charT,traits>& operator>> (basic_istream<charT,traits>& is, signed char& ch );

template <class charT, class traits>
  basic_istream<charT,traits>& operator>> (basic_istream<charT,traits>& is, unsigned char& ch );

template <class charT, class traits>
  basic_istream<charT,traits>& operator>> (basic_istream<charT,traits>& is, charT* str );

template <class charT, class traits>
  basic_istream<charT,traits>& operator>> (basic_istream<charT,traits>& is, signed char* str );

template <class charT, class traits>
  basic_istream<charT,traits>& operator>> (basic_istream<charT,traits>& is, unsigned char* str );

See also.
  get, getline, ostream::operator<<
  istream class


© The C++ Resources Network, 2001