-->

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

2020-03-01 10:27发布

问题:

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.