CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 23 Jul 2025 22:50:28 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/ostream/ostream/operator<
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Jul 2025 22:50:28 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"988c-gmtUQ0pi4YBz+tNppiXtzVKHXIM"
Content-Encoding: gzip
This operator (
See operator<< for additional overloads (as non-member functions) of this operator.
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.
Output:
Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog) when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).
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>
ostream
- ostream::~ostream
- ostream::ostream
member classes
member functions
non-member overloads
protected members
-
ostream::operator=C++11
-
ostream::swapC++11
-
public member function
<ostream> <iostream>
std::ostream::operator<<
arithmetic types (1) | ostream& operator<< (bool val);ostream& operator<< (short val);ostream& operator<< (unsigned short val);ostream& operator<< (int val);ostream& operator<< (unsigned int val);ostream& operator<< (long val);ostream& operator<< (unsigned long val);ostream& operator<< (float val);ostream& operator<< (double val);ostream& operator<< (long double val);ostream& operator<< (void* val); |
---|---|
stream buffers (2) | ostream& operator<< (streambuf* sb ); |
manipulators (3) | ostream& operator<< (ostream& (*pf)(ostream&));ostream& operator<< (ios& (*pf)(ios&));ostream& operator<< (ios_base& (*pf)(ios_base&)); |
arithmetic types (1) | ostream& operator<< (bool val);ostream& operator<< (short val);ostream& operator<< (unsigned short val);ostream& operator<< (int val);ostream& operator<< (unsigned int val);ostream& operator<< (long val);ostream& operator<< (unsigned long val);ostream& operator<< (long long val);ostream& operator<< (unsigned long long val);ostream& operator<< (float val);ostream& operator<< (double val);ostream& operator<< (long double val);ostream& operator<< (void* val); |
---|---|
stream buffers (2) | ostream& operator<< (streambuf* sb ); |
manipulators (3) | ostream& operator<< (ostream& (*pf)(ostream&));ostream& operator<< (ios& (*pf)(ios&));ostream& operator<< (ios_base& (*pf)(ios_base&)); |
Insert formatted output
<<
) applied to an output stream is known as insertion operator. It is overloaded as a member function for:- (1) arithmetic types
- Generates a sequence of characters with the representation of val, properly formatted according to the locale and other formatting settings selected in the stream, and inserts them into the output stream.
Internally, the function accesses the output sequence by first constructing a sentry object. Then (if good), it calls num_put::put (using the stream's selected locale) to perform both the formatting and the insertion operations, adjusting the stream's internal state flags accordingly. Finally, it destroys the sentry object before returning.
- (2) stream buffers
-
Retrieves as many characters as possible from the input sequence controlled by the stream buffer object pointed by sb (if any) and inserts them into the stream, until either the input sequence is exhausted or the function fails to insert into the stream.
Internally, the function accesses the output sequence by first constructing a sentry object. Then (if good), it inserts characters into its associated stream buffer object as if calling its member function sputc, and finally destroys the sentry object before returning.
- (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 output sequence and inserts no characters (unless the manipulator itself does, like endl or ends do).
See operator<< for additional overloads (as non-member functions) of this operator.
Parameters
- val
- Value to be formatted and inserted into the stream.
Notice that the type of this argument (along with the stream's format flags) influences the format of the representation inserted.
- sb
- Pointer to a streambuf object from whose controlled input 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 ostream objects are:
manipulator Effect endl Inserts a new-line character and flushes the stream. ends Inserts a null character. flush Flushes the stream. boolalpha / noboolalpha Activates/deactivates the insertion of bool
values as their alphanumerical representations.showbase / noshowbase Activates/deactivates the insertion of base prefixes in the representations of integer values. showpoint / noshowpoint Activates/deactivates the requirement that the decimal point is always written on the representations of floating-point values. showpos / noshowpos Activates/deactivates the requirement that positive values are preceded with a plus sign ( +
).unitbuf / nounitbuf Activates/deactivates automatic flushing after each insertion operation. uppercase / nouppercase Activates/deactivates the use of uppercase letters for characters used in the representations of arithmetic types. dec / hex / oct basefield setting: Sets that base used to represent integral numerical values. fixed / scientific / hexfloat / defaultfloat floatfield setting: Format used to represent floating-point values. internal / left / right adjustfield setting: Position where fill characters are inserted to adjust the field to the field width.
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 represent integral numerical values . setfill Sets the fill character used to adjust fields to the field width. setprecision Sets the decimal precision used to represent floating-point values. setw Sets the field width used by the next formatted insertion. setiosflags/resetiosflags Set/reset format flags.
Return Value
The ostream object (*this
).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 | - |
failbit | The function failed to format val (it may also be set if the construction of sentry failed). For (2), it is set if no characters could be extracted from the object pointed by sb. |
badbit | Either the insertion on the stream failed, or some other error happened (such as when this function catches an exception thrown by an internal operation). When set, the integrity of the stream may have been affected. For (2), it is also set if sb is a null pointer. |
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
Example
|
|
Output:
65 |
Data races
Modifies the object pointed by sb (2).Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog) when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).
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
- operator<< (ostream)
- Insert characters (public member function)
- ostream::put
- Put character (public member function)
- ostream::write
- Write block of data (public member function)
- istream::operator>>
- Extract formatted input (public member function)
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