Understanding STL Iterators : Ostream Iterator

Chukwunonso Nwafor
May 30 · 2 min read
C++ logo created by Jeremy Kratz

Disclaimer

Honour must be given to whom honour is due. Firstly, honour must be given to Bjarne Stroustrup for providing us with the C++ formalism with which we present our ideas. Secondly, honour must be given to Alexander Stepanov for presenting to us this style of reasoning, his ideas and hard-work have greatly changed the way programs are written.

All codes presented here are based on what the smart minds behind SGI STL have bequeathed to humanity. I do not make claim to the concepts, approaches detailed here but only provide explanations based on my understanding with the hope that it may be useful to some other programmer who cares about beautifully crafted components.

Motivation

What is a better way to put theory to test if not by implementation. I classify algorithms at Algorithmcity. Confident of my C++ skills, I applied at think-cell, I flunked the interview, I was humbled and needed to rethink my design approach. I was made to appreciate the difference between coding an algorithm and designing a useful software component. First I devoured most papers by Alexander Stepanov, then I set my eyes on the Standard Template Library and the Boost Library. This are some of the things I have learn’t when messing with this libraries.

Ostream Iterator

Ostream iterator models an output iterator. It outputs objects of a particular type to an ostream.

Code Example

first code example

Complete Code

ostream_iterator class

Code Explanation

code fragment: (line #4 — #6)

Here we defined the character type, character traits type and the stream type.

code fragment: (line #8 — #12)

Here the iterator traits of the ostream_iterator class are defined. Its iterator category is output iterator denoted by output_iterator_tag. The reference , difference type, pointer and value type are all void.

code fragment: (line #14 — #21)

Here, semi-regular operations are defined for the ostream_iterator type as well as constructors for construction.

code fragment: (line #23 — #25)

Line #23 defines dereferencing through operator*().

Line #24 defines pre-increment operator through operator++().

Line #25 defines post-increment operator through operator++(int).

Understanding STL Iterators

Detailed description of STL iterators based on the SGI STL implementation

Chukwunonso Nwafor

Written by

I love solving computational problems using C++

Understanding STL Iterators

Detailed description of STL iterators based on the SGI STL implementation

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade