Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

An Enhanced ostream_iterator


I have benefited greatly from criticism, and at no time have I suffered a lack thereof.
—Winston Churchill

I'm so gorgeous, there's a six-month waiting list for birds to suddenly appear, every time I am near!
—Cat, Red Dwarf

Introduction

Do you ever find yourself wanting to output the contents of a sequence where the elements are to be indented, perhaps by a tab space ('\ t'), as in the following example?

Header Files:
  H:\ freelibs\ b64\ current\ include\ b64\ b64.h
  H:\ freelibs\ b64\ current\ include\ b64\ cpp\ b64.hpp
Implementation Files:
  H:\ freelibs\ b64\ current\ src\ b64.c
  H:\ freelibs\ b64\ current\ test\C\C.c
  H:\ freelibs\ b64\ current\ test\Cpp\ Cpp.cpp

Using std::ostream_iterator, this is disproportionately difficult and inelegant to achieve. Consider that we're searching for source files under the current directory, using the recls/STL library (itself an adaptation of a collection in what should now be, after reading Part II, characteristic STL extension style). recls/STL provides the recls::stl::search_sequence collection (a typedef of recls::stl::basic_search_sequence<char>), to which we pass the search directory, pattern, and flags. We can use this in combination with std::copy() and std::ostream_iterator, as shown in Listing One, to achieve the desired output.

Listing One: Formatting Output Using std::ostream_iterator

typedef recls::stl::search_sequence srchseq_t;
using recls::RECLS_F_RECURSIVE;

srchseq_t headers(".", "*.h|*.hpp", RECLS_F_RECURSIVE);
srchseq_t impls(".", "*.c|*.cpp", RECLS_F_RECURSIVE);

std::cout << "Header Files:" << std::endl << "\ t";
std::copy(headers.begin(), headers.end()
, std::ostream_iterator<srchseq_t::value_type>(std::cout, "\ n\ t"));
std::cout << "\ r";

std::cout << "Implementation Files:" << std::endl << "\ t";
std::copy(impls.begin(), impls.end()
, std::ostream_iterator<srchseq_t::value_type>(std::cout, "\ n\ t"));
std::cout << "\n";

Obviously, there's a degree of mess here in that the formatting we seek to apply on lines 8–9 and 13–14 leaks out into lines 7, 10, 12. and 15. I'm certain you can imagine how, in more complex cases, this can lead to convoluted and fragile code, something that would just not happen were std::ostream_iterator a tiny bit smarter. This chapter describes how std::ostream_iterator can be enhanced in a simple but crucial way, in the form of stlsoft::ostream_ iterator.

Before we look in depth at the problem and the simple solution, let's see that solution in action (Listing Two).

Listing Two: Formatting Output Using stlsoft::ostream_iterator

typedef recls::stl::search_sequence srchseq_t;
using recls::RECLS_F_RECURSIVE;

srchseq_t headers(".", "*.h|*.hpp", RECLS_F_RECURSIVE);
srchseq_t impls(".", "*.c|*.cpp", RECLS_F_RECURSIVE);

std::cout << "Header Files:" << std::endl;
std::copy(headers.begin(), headers.end()
    , stlsoft::ostream_iterator<srchseq_t::value_type>(std::cout
                          , "\ t", "\ n"));

std::cout << "Implementation Files:" << std::endl;
std::copy(impls.begin(), impls.end()
    , stlsoft::ostream_iterator<srchseq_t::value_type>(std::cout
                          , "\t","\ n"));

Now the formatting is entirely located where it should be, in the invocation of the iterator's constructor. Naturally, this component can be used easily in formatted output that employs different levels of indentation.

(If we wanted to be especially pious with regards to DRY SPOT, we might declare a single instance of stlsoft::ostream_iterator with the required prefix and suffix and pass it to the two invocations of std::copy(). But that's not as clear-cut as you might think, as we'll shortly see.)


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.