c++11 standard template (STL) (std::basic_ios) (4)

Defined in the header file <ios>

template<

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

> class basic_ios : public std::ios_base

The class std::basic_ios provides facilities for assigning an interface to objects that have the std::basic_streambuf interface. Several std::basic_ios objects can refer to an actual std::basic_streambuf object.

inheritance diagram

 

Two specializations for common character types are also provided:

type definition
ios basic_ios<char>
wios basic_ios<wchar_t>

format

Copy formatted information

std::basic_ios<CharT,Traits>::copyfmt

basic_ios& copyfmt(const basic_ios& other);

Has no effect if other and *this refer to the same object. Otherwise copies the state otherof into *this. Proceed in the following sequence:

1) Passing erase_event as a parameter, call register_callback() for each callback registered.

2) otherCopies all member objects from to *this, except rdstate(), exceptionmask, and rdbuf(). In particular, the locale, formatting flags, the contents of the arrays std::ios_base::iword and std::ios_base::pword (but not iwordthe and pwordpointers themselves), callbacks, and streams of ties are copied.

3) Pass copyfmt_event as a parameter and call each callback registered by register_callback().

4) otherCopies the exception mask from to *this, as if by calling exceptions(other.exceptions()).

parameter

other - another stream to use as source

return value

*this

Notice

The second pass through the callback can be used to deep copy the user-defined object pointed to by the pointer in std::ios_base::pword.

copyfmt()Can be used to save and restore stream state. Boost provides a more fine-grained IO state savers library for the same purpose.

call example

#include <iostream>
#include <fstream>

int main()
{
    std::ofstream out;

    out.copyfmt(std::cout); // 复制 rdstate 和 rdbuf 外的所有内容
    out.clear(std::cout.rdstate()); // 复制 rdstate
    out.basic_ios<char>::rdbuf(std::cout.rdbuf()); // 共享缓冲

    out << "Hello, world\n";
}

output

 

Manage Fill Characters

std::basic_ios<CharT,Traits>::fill

CharT fill() const;

(1)

CharT fill( CharT ch );

(2)

Manages padding characters used to pad input transitions to the specified width.

1) Returns the current fill character

2) Set the fill character to ch, return the previous value of the fill character

parameter

ch - character to use as padding character

return value

Padding characters before calling the function.

call example

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << "With default setting : " << std::setw(10) << 40 << '\n';
    char prev = std::cout.fill('x');
    std::cout << "Replaced '" << prev << "' with '"
              << std::cout.fill() << "': " << std::setw(10) << 40 << '\n';
}

output

 

おすすめ

転載: blog.csdn.net/qq_40788199/article/details/131276585