Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios and ios_base class for stream formatting

Tags:

c++

iostream

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?

like image 290
sevenOfNine Avatar asked Oct 02 '13 00:10

sevenOfNine


1 Answers

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.

like image 84
Dietmar Kühl Avatar answered Sep 19 '22 13:09

Dietmar Kühl