operator>>(std::basic_istream)
Defined in header <istream>
|
||
template< class CharT, class Traits > basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT& ch ); |
(1) | |
(2) | ||
template< class CharT, class Traits> basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT* s ); |
(until C++20) | |
template< class CharT, class Traits, std::size_t N > basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT (&s)[N] ); |
(since C++20) | |
template< class Istream, class T > Istream&& operator>>( Istream&& st, T&& value ); |
(3) | (since C++11) |
ch
. If no character is available, sets failbit (in addition to eofbit that is set as required of a FormattedInputFunction).s
. 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.
|
(until C++20) |
|
(since C++20) |
- end of file occurs in the input sequence (this also sets eofbit)
Istream
is a class type publicly and unambiguously derived from std::ios_base.Notes
Extracting a single character that is the last character of the stream does not set eofbit
: this is different from other formatted input functions, such as extracting the last integer with operator>>, but this behavior matches the behavior of std::scanf with "%c" format specifier.
Parameters
st | - | input stream to extract the data from |
ch | - | reference to a character to store the extracted character to |
s | - | pointer to (until C++20) a character array to store the extracted characters to |
Return value
st
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
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 1203 | C++11 | overload for rvalue stream returned lvalue reference to the base class | returns rvalue reference to the derived class |
LWG 2328 | C++11 | overload for rvalue stream required another argument to be lvalue | made to accept rvalue |
LWG 2534 | C++11 | overload for rvalue stream was not constrained | constrained |
See also
extracts formatted data (public member function) |