Home arrow C++ arrow 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++
(Page 1 of 4 )

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.


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