C++
  Home arrow C++ arrow Page 2 - Custom Stream Manipulation in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Custom Stream Manipulation in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-05-05

    Table of Contents:
  • Custom Stream Manipulation in C++
  • Manipulators with No Argument
  • Solution for Multiple Arguments
  • Templates on the Fly

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek