c++11 standard template (STL) (std::basic_ostream) (2)

Defined in the header file <ostream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_ostream : virtual public std::basic_ios<CharT, Traits>

Class template basic_ostreamProvides 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_streambufis implemented with the interface provided by the class, basic_iosaccessed through the base class. In a typical implementation, basic_ostreamthere 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::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). (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\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::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();
}

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\n";
    }
    ~Foo()
    {
        std::clog << "static destructor\n";
    }
};

Foo f; // 静态对象

int main()
{
    std::clog << "main function\n";
}

output

 

Guess you like

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