c++11 standard template (STL) pre-defines standard stream objects

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::cinand std::wcincontrol 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::cinThis 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 ); cinmeans "character input" and wcinmeans "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::coutand std::wcoutcontrol 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::coutis tied() to std::cin and std::wcouttied() 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::coutalso tie()'d to std::cerr and std::wcouttied() 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 ); coutmeans "character output" and wcoutmeans "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::cerrand std::wcerrcontrol 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 ( wcerras 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 wcerrand 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 ); cerrmeans "character error (stream)" and wcerrmeans "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::clogand std::wclogcontrol 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 ); clogmeans "character log" and wclogmeans "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;
}

output

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/132630322