ios and ios_base class for stream formatting

最后都变了- 提交于 2019-12-10 04:16:59

问题


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;
}

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/19128054/ios-and-ios-base-class-for-stream-formatting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!