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.Before you venture any further, let me point out that this article is part of a larger series, each part of which will appear here on the Developer Shed Network. I invite you to read the earlier parts first to perfect your understanding of streams. The part directly connected to this one is titled General Stream Manipulation in C++.
If you are only interested in how to create a custom manipulator, keep reading; otherwise, you'll want to read the earlier part. It covers what a manipulator is and how it works in general, so it serves as an excellent introduction for this article.
You may ask why it is that the manipulators that do not require arguments are in the iostream header, and the others are inside the iomanip header. This is due the fact that it is a little more complicated to pass a stream as an argument via the << operator inside a function. To achieve this, you need to declare a new class and functions that will operate behind the scene.
That can consume quite a bit of space if you need to do it for a large list of manipulators. However, we should not rush so far ahead. Let me just state that you do this in separate ways for a small number of parameters and for a large number of parameters.
You might require a custom manipulator for one of two reasons. First, you may want to group several already-written ones inside a new manipulator. In this way, each time you call the custom manipulator, all of the ones within it will be applied, allowing you to achieve the same result with less code, which may lead to a more readable view of the code snippet. Secondly, you may want to do something that is not implemented in the library.