I recently had a problem creating a stringstream
due to the fact that I incorrectly assumed std::setw()
would affect the stringstream for every ins
setw()
only affects the next insertion. That's just the way setw()
behaves. The behavior of setw()
is the same as ios_base::width()
. I got my setw()
information from cplusplus.com.
You can find a full list of manipulators here. From that link, all the stream flags should say set until changed by another manipulator. One note about the left
, right
and internal
manipulators: They are like the other flags and do persist until changed. However, they only have an effect when the width of the stream is set, and the width must be set every line. So, for example
cout.width(6);
cout << right << "a" << endl;
cout.width(6);
cout << "b" << endl;
cout.width(6);
cout << "c" << endl;
would give you
> a
> b
> c
but
cout.width(6);
cout << right << "a" << endl;
cout << "b" << endl;
cout << "c" << endl;
would give you
> a
>b
>c
The Input and Output manipulators are not sticky and only occur once where they are used. The parametrized manipulators are each different, here's a brief description of each:
setiosflags lets you manually set flags, a list of which can be fount here, so it is sticky.
resetiosflags behaves the similar to setiosflags except it unsets the specified flags.
setbase sets the base of integers inserted into the stream (so 17 in base 16 would be "11", and in base 2 would be "10001").
setfill sets the fill character to insert in the stream when setw
is used.
setprecision sets the decimal precision to be used when inserting floating point values.
setw makes only the next insertion the specified width by filling with the character specified in setfill