What's the opposite of `fixed` in cout?

折月煮酒 提交于 2019-12-18 20:13:09

问题


When using cout, what is the default formatter defined in the <iomanip> header? In other words, once I've set my formatter to fixed using cout << fixed << setPrecision(2), how do I change it back? Or, what am I changing it back to?


回答1:


The opposite of std::fixed is std::scientific.

(You find a nice list of manipulators in this great answer.)




回答2:


The answer is std::defaultfloat in C++11. To achieve this in C++03 you can do

cout.unsetf(std::ios_base::floatfield);

See Really, what's the opposite of "fixed" I/O manipulator?




回答3:


You can use resetiosflags() to unset any flags.




回答4:


The opposite of std::fixed is std::scientific. That might do for you.

However, if you want to restore more flags, or if you need the previous state, instead of the default you can use better solutions:

  1. the std::resetiosflags manipulator lets you reset specific flags to their defaults;

  2. the two ios::flags functions let you save and restore the previous values of the format flags.



来源:https://stackoverflow.com/questions/7422200/whats-the-opposite-of-fixed-in-cout

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