Operators are helper functions that enable code to control input/output streams using operator<< or operator>>.
Manipulators called without arguments (such as std::cout << std::boolalpha; or std::cin >> std::hex; ) are implemented as functions that accept a reference to a stream as their only argument. Special overloaded versions of basic_ostream::operator<< and basic_istream::operator>> accept pointers to these functions. These functions (or instantiations of function templates) are the only addressable functions in the standard library. (since C++20)
Manipulators called with arguments (such as std::cout << std::setw(10); ) are implemented as functions that return an object of unspecified type. These operators define their own operator<< or operator>> that performs the requested operation.
Controls whether some output operations use uppercase letters
std::uppercase,
std::nouppercase
std::ios_base& uppercase( std::ios_base& str ); |
(1) | |
std::ios_base& nouppercase( std::ios_base& str ); |
(2) |
Enables the use of uppercase characters in floating point and hexadecimal integer output. Has no effect on input.
1) Enable the flag str
in the stream as by calling str.setf(std::ios_base::uppercase)uppercase
2) Just like calling str.unsetf(std::ios_base::uppercase) to disable the flag str
in the streamuppercase
This is an I/O manipulator that can be used with an expression such as out << std::uppercase for any std::basic_ostream type out
or with an expression such as in >> std::uppercase for any std::basic_istream type. in
transfer.
parameter
str | - | Reference to I/O stream |
return value
str
(reference to the manipulated stream)
Call example
#include <iostream>
int main()
{
std::cout << std::hex << std::showbase
<< "0x2a with uppercase: "
<< std::uppercase << 0x2a << std::endl
<< "0x2a with nouppercase: "
<< std::nouppercase << 0x2a << std::endl
<< "1e-10 with uppercase: "
<< std::uppercase << 1e-10 << std::endl
<< "1e-10 with nouppercase: "
<< std::nouppercase << 1e-10 << std::endl;
return 0;
}
output
Controls whether the output is flushed after each operation
std::unitbuf,
std::nounitbuf
std::ios_base& unitbuf( std::ios_base& str ); |
(1) | |
std::ios_base& nounitbuf( std::ios_base& str ); |
(2) |
Enables or disables automatic flushing after any output operation. Has no effect on input.
1) Enable the flag str
in the stream as by calling str.setf(std::ios_base::unitbuf)unitbuf
2) Just like calling str.unsetf(std::ios_base::unitbuf) to disable the flag str
in the streamunitbuf
This is an I/O manipulator that can be used with an expression such as out << std::unitbuf for any std::basic_ostream type out
or with an expression such as in >> std::unitbuf for any std::basic_istream type. in
transfer.
annotation
Inject in the destructor of the std::basic_ostream::sentry object. If str.flags() & std::ios_base::unitbuf is true, the destructor calls str.rdbuf()->pubsync().
The standard output objects std::cerr and std::wcerr have their unitbuf
bits set by default.
parameter
str | - | Reference to I/O stream |
return value
str
(reference to the manipulated stream)
Call example
#include <iostream>
#include <chrono>
#include <iostream>
template<typename Diff>
void log_progress(Diff d)
{
std::cout << "..("
<< std::chrono::duration_cast<std::chrono::milliseconds>(d).count()
<< " ms)..";
std::cout << std::endl;
}
int main()
{
volatile int sink = 0;
std::cout << std::unitbuf; // 启用自动冲入
auto t1 = std::chrono::high_resolution_clock::now();
for (int j = 0; j < 5; ++j)
{
for (int n = 0; n < 10000; ++n)
{
for (int m = 0; m < 20000; ++m)
{
sink += m * n; // 做一些工作
}
}
auto now = std::chrono::high_resolution_clock::now();
log_progress(now - t1);
}
std::cout << std::endl;
return 0;
}