使用 cout
时,在 <iomanip>
头文件中默认定义了什么格式化程序?换句话说,一旦我使用 cout << fixed << setPrecision(2)
将格式化程序设置为 fixed
,我该如何将其改回来?或者说,我要将其改回到什么状态?
std::defaultfloat
。在C++03中,您可以执行以下操作来实现:
cout.unsetf(std::ios_base::floatfield);
std::fixed
的相反是std::scientific
。
(您可以在这个很棒的回答中找到一份不错的操作符列表。)
您可以使用resetiosflags()
来取消设置任何标志。
std::ios::scientific
。 - Chadiword
和pword
。(当然,James完全意识到这些限制。)但是,您可以通过std::ios::flags()
取得相当大的进展。 - sbistd::fixed
的相反是std::scientific
,这可能适合您。
然而,如果您想要恢复更多标志,或者如果需要先前的状态,而不是默认状态,则可以使用更好的解决方案:
std::resetiosflags
操作器可让您将特定标志重置为其默认值;
两个ios::flags
函数让您保存和恢复格式标志的先前值。
std::fixed
的相反是不是std::scientific
。它更像是一种自动格式切换模式。 - Cheers and hth. - Alf
cout.unsetf(std::ios_base::floatfield);
this working with C++11 but notstd::defaultfloat
- Abhishek Manecout.unsetf(std::ios_base::floatfield);
,为什么它只是反转了fixed
的效果,而没有包含任何像fixed
这样的关键字,这意味着为什么它不重置precision
。 - Abhishek Mane