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:
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.
- 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;
Parameters.
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:
boolalpha consider alphabetic sequences true and false as valid bool type values. dec interpret numeric values in decimal format. hex interpret numeric values in hexadecimal format. noboolalpha do not consider alphabetic sequences true and false as valid bool type values. noskipws do not skip possible white spaces before extractions. oct interpret numeric values in octal format. skipws skip any possible white space before extractions. ws discard 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&)); |
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