Predefined standard stream objects
read from the standard C input stream stdin
std::cin,
std::wcin
extern std::istream cin; |
(1) | |
extern std::wistream wcin; |
(2) |
The global objects std::cin
and std::wcin
control input from a stream buffer of implementation-defined type (derived from std::streambuf ), associated with the standard C input stream stdin.
These objects are guaranteed to be initialized before or during first construction of std::ios_base::Init, and they can be used in constructors and destructors of static objects with ordered initialization (as long as they are included before the object is defined) <iostream>
.
Accessing these objects from multiple threads, for both formatted and unformatted input, is safe if sync_with_stdio(false) is not used.
Once constructed std::cin
, std::cin.tie() returns &std::cout, and similarly std::wcin.tie() returns &std::wcout. std::cin
This means that any formatted input operation on will force a call to std::cout.flush() if any characters are pending output .
Notice
The 'c' in the name refers to "character" ( stroustrup.com FAQ ); cin
means "character input" and wcin
means "wide character input".
call example
#include <iostream>
struct Foo
{
int n;
Foo()
{
std::cout << "Enter n: "; // 不需要冲入
std::cin >> n;
}
};
Foo f; // static object
int main()
{
std::cout << "f.n is " << f.n << '\n';
return 0;
}
output
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). (since 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" << std::endl;
return 0;
}
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();
return 0;
}
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" << std::endl;
}
~Foo()
{
std::clog << "static destructor" << std::endl;
}
};
Foo f; // 静态对象
int main()
{
std::clog << "main function" << std::endl;
}