Home arrow C++ arrow Page 2 - Custom Stream Manipulation in C++
C++

Custom Stream Manipulation in C++


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.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
May 05, 2009
TABLE OF CONTENTS:
  1. · Custom Stream Manipulation in C++
  2. · Manipulators with No Argument
  3. · Solution for Multiple Arguments
  4. · Templates on the Fly

print this article
SEARCH DEVARTICLES

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

return (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:


#include <iostream>

#include <iomanip>

#include <string>


using namespace std;


inline ios_base& scientify(ios_base& in)

{

in.setf( ios_base::showpos );

in.setf( ios_base::scientific | ios_base::showpoint);

return(in);

}


inline ios_base& resetFloat(ios_base& in)

{

in.setf( 0, ios_base::floatfield);

return(in);

}


int main( ) {


ios_base::fmtflags oldFlags = cout.flags( );


 

double i = 3.165;

 

cout << setprecision( 3);

cout << scientify << i << endl;


cout<< resetFloat << i << endl;


cout.flags(oldFlags);

}


+3.165e+000

+3.17

Press any key to continue . . .


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.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials