Defined in the header file <ostream>
template< class CharT, |
Class template basic_ostream
Provides high-level output operations on character streams. Supported operations include formatted output (such as integer values) and unformatted output (such as raw characters and character arrays). This functionality basic_streambuf
is implemented with the interface provided by the class, basic_ios
accessed through the base class. In a typical implementation, basic_ostream
there are no non-inherited data members.
global object
The standard library provides six global basic_ostream objects.
Writes to the standard C output stream stdout
std::cout,
std::wcout
extern std::ostream cout; |
(1) | |
extern std::wostream wcout; |
(2) |
The global objects std::cout
and std::wcout
control the output to an implementation-defined type of stream buffer (derived from std::streambuf ), which is associated with the standard C output stream stdout.
These objects are guaranteed to be initialized during or before the first construction of an object of type std::ios_base::Init and are available to constructors and destructors of static objects with ordered initialization (as long as they are included before the object is defined) <iostream>
.
Unless sync_with_stdio(false) is issued, it is safe to access these objects concurrently from multiple threads for both formatted and unformatted output.
Once initialized, std::cout
is tied() to std::cin and std::wcout
tied() to std::wcin, which means that any input operation on std::cin performs std::cout.flush() (via std::basic_istream ::sentry constructor).
Once initialized, is std::cout
also tie()'d to std::cerr and std::wcout
tied() to std::wcerr, which means that any input operation on std::cerr performs std::cout.flush() (via std:: basic_istream::sentry constructor). (C++11)
Notice
The 'c' in the name refers to "character" ( stroustrup.com FAQ ); cout
means "character output" and wcout
means "wide character output".
call example
#include <iostream>
struct Foo
{
int n;
Foo()
{
std::cout << "static constructor\n";
}
~Foo()
{
std::cout << "static destructor\n";
}
};
Foo f; // 静态对象
int main()
{
std::cout << "main function\n";
}
output
Writes to the standard C error stream stderr, unbuffered
std::cerr,
std::wcerr
extern std::ostream cerr; |
(1) | |
extern std::wostream wcerr; |
(2) |
The global objects std::cerr
and std::wcerr
control stream buffers to implementation-defined types (derived from std::streambuf and std::wstreambuf respectively), associated with the standard C error output stream stderr.
These objects are guaranteed to be initialized before or during construction of the first object of type std::ios_base::Init, and are available to constructors and destructors of static objects with ordered initialization (as long as they are included before the object is defined) <iostream>
.
Unless sync_with_stdio(false) is issued, it is safe to access these objects from multiple threads for both formatted and unformatted output.
Once initialized, (std::cerr.flags() & unitbuf) != 0 ( wcerr
as well), which means that any output sent to these stream objects is immediately flushed to the OS (via std::basic_ostream::sentry destructor).
Additionally, std::cerr.tie() returns &std::cout (same for wcerr
and wcout
), which means that any output on std::cerr first executes std::cout.flush() (via std::basic_ostream::sentry constructor). (since C++11)
Notice
The 'c' in the name refers to "character" ( stroustrup.com FAQ ); cerr
means "character error (stream)" and wcerr
means "wide character error (stream)".
call example
#include <thread>
#include <iostream>
#include <chrono>
void f()
{
std::cout << "Output from thread...";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "...thread calls flush()" << std::endl;
}
int main()
{
std::thread t1(f);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::clog << "This output from main is not tie()'d to cout\n";
std::cerr << "This output is tie()'d to cout\n";
t1.join();
}
output
Writes to the standard C error stream stderr
std::clog,
std::wclog
extern std::ostream clog; |
(1) | |
extern std::wostream wclog; |
(2) |
global object std::clog
and std::wclog
control stream buffers of implementation-defined type (derived from std::streambuf), associated with the standard C output stream stderr, but unlike std::cerr/std::wcerr, not automatically flushed into these streams, and not automatically with cout tie().
These objects are guaranteed to be initialized before or during construction of the first object of type std::ios_base::Init, and are available to constructors and destructors of static objects with ordered initialization (as long as they are included before the object is defined) <iostream>
.
Unless sync_with_stdio(false) is issued, it is safe to access these objects from multiple threads for both formatted and unformatted output.
Notice
The 'c' in the name refers to "character" ( stroustrup.com FAQ ); clog
means "character log" and wclog
means "wide character log".
call example
#include <iostream>
struct Foo
{
int n;
Foo()
{
std::clog << "static constructor\n";
}
~Foo()
{
std::clog << "static destructor\n";
}
};
Foo f; // 静态对象
int main()
{
std::clog << "main function\n";
}
output