问题
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