CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Jul 2025 00:37:54 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/istream/istream/operator>>/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 24 Jul 2025 00:37:54 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"9ae0-edKGAdz6vpzVCrB7TMsQZnD16Mo"
Content-Encoding: gzip
This operator (
See operator>> for additional overloads (as non-member functions) of this operator.
Except where stated otherwise, calling this function does not alter the value returned by member gcount.
The extracted value or sequence is not returned, but directly stored in the variable passed as argument.
Errors are signaled by modifying the internal state flags, except for (3), that never sets any flags (but the particular manipulator applied may):
Multiple flags may be set by a single operation.
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
This example demonstrates the use of some of the overloaded operator>> functions shown above using the standard istream object cin.
Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream object cin when this is synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which extracted characters are attributed to threads).
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<istream>
class templates
classes
manipulators
istream
- istream::~istream
- istream::istream
member classes
member functions
non-member overloads
protected members
-
istream::operator=C++11
-
istream::swapC++11
-
public member function
<istream> <iostream>
std::istream::operator>>
arithmetic types (1) | 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); |
---|---|
stream buffers (2) | istream& operator>> (streambuf* sb ); |
manipulators (3) | istream& operator>> (istream& (*pf)(istream&));istream& operator>> (ios& (*pf)(ios&));istream& operator>> (ios_base& (*pf)(ios_base&)); |
arithmetic types (1) | 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>> (long long& val);istream& operator>> (unsigned long long& val);istream& operator>> (float& val);istream& operator>> (double& val);istream& operator>> (long double& val);istream& operator>> (void*& val); |
---|---|
stream buffers (2) | istream& operator>> (streambuf* sb ); |
manipulators (3) | istream& operator>> (istream& (*pf)(istream&));istream& operator>> (ios& (*pf)(ios&));istream& operator>> (ios_base& (*pf)(ios_base&)); |
Extract formatted input
>>
) applied to an input stream is known as extraction operator. It is overloaded as a member function for:- (1) arithmetic types
- Extracts and parses characters sequentially from the stream to interpret them as the representation of a value of the proper type, which is stored as the value of val.
Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set tofalse
). Then (if good), it calls num_get::get (using the stream's selected locale) to perform both the extraction and the parsing operations, adjusting the stream's internal state flags accordingly. Finally, it destroys the sentry object before returning.
- (2) stream buffers
-
Extracts as many characters as possible from the stream and inserts them into the output sequence controlled by the stream buffer object pointed by sb (if any), until either the input sequence is exhausted or the function fails to insert into the object pointed by sb.
The function is considered to perform formatted input: Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set tofalse
). Then (if good), it extracts characters from its associated stream buffer object as if calling its member functions sbumpc or sgetc, and finally destroys the sentry object before returning.
The function is considered to perform unformatted input: Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set totrue
). Then (if good), it extracts characters from its associated stream buffer object as if calling its member functions sbumpc or sgetc, and finally destroys the sentry object before returning.
The number of characters successfully read and stored by this function can be accessed by calling member gcount.
- (3) manipulators
- Calls
pf(*this)
, where pf may be a manipulator.
Manipulators are functions specifically designed to be called when used with this operator.
This operation has no effect on the input sequence and extracts no characters (unless the manipulator itself does, like ws).
See operator>> for additional overloads (as non-member functions) of this operator.
Except where stated otherwise, calling this function does not alter the value returned by member gcount.
Parameters
- val
- Object where the value that the extracted characters represent is stored.
Notice that the type of this argument (along with the stream's format flags) influences what constitutes a valid representation.
- sb
- Pointer to a streambuf object on whose controlled output sequence the characters are copied.
- pf
- A function that takes and returns a stream object. It generally is a manipulator function.
The standard manipulators which have an effect when used on standard istream objects are:
manipulator Effect ws Extracts whitespaces. boolalpha/noboolalpha Activates/deactivates the extraction of alphanumerical representations of values of type bool
.skipws/noskipws Activates/deactivates whether leading whitespaces are discarded before formatted input operations. dec/hex/oct Sets that base used to interpret integral numerical values.
The following extended manipulators can also be applied to istream objects (these take additional arguments and require the explicit inclusion of the <iomanip> header):
manipulator Effect setbase Sets the numerical base used to interpret integral numerical values. setiosflags/resetiosflags Set/reset format flags.
Return Value
The istream object (*this
).The extracted value or sequence is not returned, but directly stored in the variable passed as argument.
Errors are signaled by modifying the internal state flags, except for (3), that never sets any flags (but the particular manipulator applied may):
flag | error |
---|---|
eofbit | The input sequence has no more characters available (end-of-file reached). |
failbit | Either no characters were extracted, or the characters extracted could not be interpreted as a valid value of the appropriate type. For (2), it is set when no characters are inserted in the object pointed by sb, or when sb is a null pointer. |
badbit | Error on stream (such as when this function catches an exception thrown by an internal operation). When set, the integrity of the stream may have been affected. |
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
Example
|
|
This example demonstrates the use of some of the overloaded operator>> functions shown above using the standard istream object cin.
Data races
Modifies val or the object pointed by sb.Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream object cin when this is synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which extracted characters are attributed to threads).
Exception safety
Basic guarantee: if an exception is thrown, the object is in a valid state.It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.
See also
- istream::get
- Get characters (public member function)
- istream::getline
- Get line (public member function)
- ostream::operator<<
- Insert formatted output (public member function)
- istream::sentry
- Prepare stream for input (public member class)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us