11.9 OUR OWN MANIPULATORS
We can write our own manipulators. As they are written for specific situations, they are also called as custom manipulators.
Manipulators are designed as functions. It must return a reference to ostream. As we cannot return a local variable as reference, we have to pass reference to ostream as a parameter. It helps it to take part in a chained output.
Consider the problem of printing currency. We need the precision of two. In addition, we want trailing zeros, if any. To define a manipulator, a particular format is needed. It is shown below.
Problem: Write a program to define a manipulator “standard”. It should help in setting suitable flags for printing floating point number as amount.
Solution: Amounts are written only ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access