What's the opposite of `fixed` in cout?
Asked Answered
C

4

15

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?

Civilian answered 14/9, 2011 at 19:53 Comment(0)
H
6

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

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

Hatchery answered 14/9, 2011 at 19:56 Comment(1)
The default float and the scientific notation are different. It is not exactly "changing it back".Ats
O
17

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?

Octahedron answered 27/9, 2013 at 3:12 Comment(2)
cout.unsetf(std::ios_base::floatfield); this working with C++11 but not std::defaultfloatBoxing
and also cout.unsetf(std::ios_base::floatfield); why it only reverse the effect of fixed as it not include any keyword like fixed means why it not reset precision alsoBoxing
H
6

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

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

Hatchery answered 14/9, 2011 at 19:56 Comment(1)
The default float and the scientific notation are different. It is not exactly "changing it back".Ats
H
1

You can use resetiosflags() to unset any flags.

Hyposthenia answered 14/9, 2011 at 19:55 Comment(4)
Is there a "default" that I can set to?Civilian
I believe the default is std::ios::scientific.Hyposthenia
@Moshe: Unfortunately, there is no simple way to fully reset a stream. Even the most elaborate code I have seen to do that (by James Kanze, more than a decade ago) misses on some esoteric propertiesm, like iword and pword. (Of course, James was fully aware of the limitations.) You can, however, get pretty far with std::ios::flags().Hatchery
The easiest way to reset a stream is to default construct it. In some cases it may make sense to format your output in an ostringstream and then just send the already formatted output to cout as a std::string. It may be more costly to do it this way (output is not usually a place where I spend a lot of time optimizing or investigating performance issues, as usually other hardware related concerns outweigh any performance decreases).Hyposthenia
D
1

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.

Duwe answered 14/9, 2011 at 20:0 Comment(1)
uhm, in the sense of "go back to default" the opposite of std::fixed is not std::scientific. it is more like an automatic format-switching mode.Cathead

© 2022 - 2024 — McMap. All rights reserved.