ios和ios_base类用于流格式化 [英] ios and ios_base class for stream formatting

查看:66
本文介绍了ios和ios_base类用于流格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有两种方法可以设置iostream的setf()/unsetf(),即 (1)ios和(2)ios_base.

I found there is two ways to setf()/unsetf() for the iostream, that is (1) ios and (2) ios_base.

#include <iostream>
using namespace std;

int main() {
    cout.width(5);
    cout << 123 << endl;

    cout.setf(ios::adjustfield); // (1) using ios::
    cout << 123 << endl;

    cout.width(5);
    cout << 456 << endl;

    cout.setf(ios_base::adjustfield); // (2) using ios_base::
    cout << 456 << endl;

    return 0;
}

当我想更改ostream的格式时,它们有什么区别?

What's the difference of them when I would like to change the format of the ostream;

您通常在更改格式时使用哪种?

Which do you use normally in changing the format?

推荐答案

常量实际上是在std::ios_base中定义的,但是std::ios(实际上是std::basic_ios<cT, Traits>)是从std::ios_base派生的.因此,可以使用std::ios访问std::ios_base中定义的所有成员.

The constants are actually defined in std::ios_base but std::ios (well, actually std::basic_ios<cT, Traits>) is derived from std::ios_base. Thus, all members defined in std::ios_base can be accessed using std::ios.

std::ios_base包含完全独立于流的template参数的所有成员. std::basic_ios<cT, Traits>std::ios_base派生并定义输入和输出流之间共有的所有成员.

The class std::ios_base contains all members which entirely independent of the stream's template parameter. std::basic_ios<cT, Traits> derives from std::ios_base and defines all members which are common between input and output streams.

这篇关于ios和ios_base类用于流格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆