operator>>(std::basic_istream)

From Cppreference

Jump to: navigation, search
template< class CharT, class Traits >

basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT& ch );

template< class Traits >
basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, signed char& ch );

template< class Traits >

basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, unsigned char& ch );
(1)
template< class CharT, class Traits>

basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT* s );

template< class Traits >
basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, signed char* s );

template< class Traits >

basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, unsigned char* s );
(2)
template< class CharT, class Traits, class T >
basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>&& st, T& value );
(3) (since C++11)

Performs unformatted input operations.

1) Extracts a character and stores it to ch.

2) Extracts a character string and stores it to s. The resulting string is null-terminated. The extraction stops if one of the following conditions are met:

  • a whitespace character (as determined by the ctype<CharT> facet) is found. The whitespace character is not extracted.
  • this->width() - 1 characters are extracted

3) Calls the appropriate extraction operator, given an rvalue reference to an input stream object (equivalent to st >> value).

Contents

[edit] Notes

The (1-2) 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.

[edit] Parameters

st - input stream to extract the data from
ch - reference to a character to store the extracted character to
s - pointer to a character string to store the extracted characters to

[edit] Return value

st

[edit] Example

#include <iostream>
#include <iomanip>
#include <sstream>
 
int main()
{
    std::string input = "n greetings";
    std::istringstream stream(input);
    char c;
    const int MAX = 6;
    char cstr[MAX];
 
    stream >> c >> std::setw(MAX) >> cstr;
    std::cout << "c = " << c << '\n'
              << "cstr = " << cstr << '\n';
 
    double f;
    std::istringstream("1.23") >> f; // rvalue stream extraction
    std::cout << "f = " << f << '\n';
}

Output:

c = n
cstr = greet
f = 1.23

[edit] See also

extracts formatted data
(public member function)