ios和ios_base类用于流格式化 [英] ios and ios_base class for stream formatting
问题描述
我发现有两种方法可以设置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屋!