Using streams in C++ is an easy and intuitive task. However, sometimes you may want to customize the input/output on a deeper level. This is possible in a powerful and easy way with the use of the manipulators.
If you've programmed in C already and used the “scanf” function, you already know what I am talking about here. Writing a float value on the screen with two digits of precision after the decimal point is done on the fly. A strong library such as C++ must have something similar implemented.
Once you've gone through this article, you will see that even more. You can go as far as to create your own formatters/manipulators, but that will be the subject of a different article, so stay tuned for that one.
For now let’s see what we have implemented. Manipulators are functions that are applied with the insertion operator << in the case of output streams. They are declared in the <iostream>, <ios> or the <iomanip> header, depending on the area they are covering.
Eventually manipulators modify a couple of flags that define the behavior of the stream, defined all the way up in the ios_base class. Nevertheless, before we apply any of them, we should first grasp the knowledge of how we can reset all that we have modified.
The “flags” member function of each stream is overloaded in two ways. First, one takes no parameters and returns the current format state. The return type is a defined type inside the stream class, namely “fmtflags.” I will present my examples on the cin and cout stream connected to the console window, but they will work on any stream class.Each time you want to save it, just act as follows:
cout.flags(usedFlag); // recall the format used at (1)
// Print what you want with the first collection
cout.flags(basicFlag); // reset the console stream flags
This first example I’ve extended a little so you can see the header I use, and that I also added the std namespace to the global namespace, as I’m a bit too lazy to write the “std::” keyword in front of each function used.
Now we have saved in the “basicFlag” variable, in matter of effect, the current format setting applied to the cout stream. The second overload of the flags function will handle the set process. It will also return the current collection of flag settings inside the stream and set the new collection that you gave as input.
As you may expect, you do not necessarily need to save the usedFlag before the change. You could also just call it as we did in the end. We set the new settings, in fact the basic settings, saved at the start.
As far as the library where you’ll find them, most of the manipulators are found inside the iostream header; however, the ones taking an argument also will have their place in the “iomanip” header, so make sure to include that as well, once you use one of them on your stream.
All of the flag changes can be resolved with the rdstate() and setstate() member functions of streams, but this is harder to read and manage. Manipulators provide a more elegant solution, so I recommend using them; however, be aware that when using these, the changes are permanent (except one that confirms the rule :P, but we will talk about that also).
If you set a flag, it will stay until you explicitly modify it. The cin/cout streams are a prime example of this, as most programmers would like to have them working in a default mode. So in those situations where you use modifiers, make sure you delimit the modifications with a state save and a restore.
If you thought that there were already too many ways available to modify them, let me say to you that the flag sets can be done with the setf() function also, and unsetf() will turn off a flag. As in the example below; do not think about what it does, just observe the method:
cout.setf( ios_base::dec );
cout.unsetf( ios_base::dec );
In fact, the “setf” has an overload that takes two parameters, where the second is the flags that are to be turned off. This trick can save you a couple of lines when you want to turn some flags on/off at the same time.