The creators of the STL library have surely gone beyond the expectations of many programmers. However, as perfect and complete as their library may seem at first, you will slowly discover that even they could not cover everything. That's when it is time for you to learn how to create a custom class/function that suits your specific needs.
Custom Stream Manipulation in C++ - Manipulators with No Argument (Page 2 of 4 )
When your operation requires no additional parameters, and you want, for instance, to pack a couple of these kinds of objects together, the task is quite trivial. The idea is to write a function that will conform to the following example:
ios_base& scientify(ios_base& in)
// make the flag modifications on in
An additional improvement can be made; it is, however, not mandatory. As you expect that this will be heavily used, you may decide to make them inline functions in order to speed up your application. Also, note that the insertion and extraction operators are not defined for the “ios_base” class, so we need to use the internal function setf to apply the flags.
Inside the setf function, the flags can be combined with the bitwise or operator. Look over my example below:
The output speaks for itself if you are already familiar with manipulators in general. During the “scientify” manipulator declaration, I wanted to point out that you can combine them and apply them in a row. The second function uses the setf function's overload, where the second argument stays for flags to be set off. The ios_base::float is a combination of flags that includes all float-related modifications. Simply calling the unsetf would raise the same result.