Sharing a Generic Ostream Iterator

C

cdiggins

The ostream_iterator from the standard library is a template. This
results in a somewhat inelegant syntax. I thought I would share the
following alternative ostream_iterator, which avoids the neccessity for
template instantion

// public domain code by Christopher Diggins

#include <iostream>

struct putter {
putter(const putter& x) : o(x.o), delim(x.delim) { }
putter(std::eek:stream& x = std::cout, const char* s = "")
: o(x), delim(s)
{ }
template<typename T>
putter& operator=(const T& x) {
o << x << delim; return *this;
}
putter& operator*() { return *this; }
putter& operator++() { return *this; }
putter& operator++(int) { return *this; }
mutable std::eek:stream& o;
const char* delim;
};

putter put(std::eek:stream& o = std::cout, const char* delim = "") {
return putter(o, delim);
}

usage is as follows:

int main() {
int array[] = { 1, 2, 4, 8, 16, 32, 64 };
std::copy(array, array + 7, put());
return 0;
}

Hope this is useful.

Christopher Diggins
http://www.cpp-cookbook.com - C++ Cookbook
 
N

Neil Cerutti

The ostream_iterator from the standard library is a template. This
results in a somewhat inelegant syntax. I thought I would share the
following alternative ostream_iterator, which avoids the neccessity for
template instantion

// public domain code by Christopher Diggins

#include <iostream>

struct putter {
putter(const putter& x) : o(x.o), delim(x.delim) { }
putter(std::eek:stream& x = std::cout, const char* s = "")
: o(x), delim(s)
{ }
template<typename T>
putter& operator=(const T& x) {
o << x << delim; return *this;
}
putter& operator*() { return *this; }
putter& operator++() { return *this; }
putter& operator++(int) { return *this; }
mutable std::eek:stream& o;
const char* delim;
};

Member o needn't be mutable, need it?
putter put(std::eek:stream& o = std::cout, const char* delim = "") {
return putter(o, delim);
}

usage is as follows:

int main() {
int array[] = { 1, 2, 4, 8, 16, 32, 64 };
std::copy(array, array + 7, put());
return 0;
}

Hope this is useful.

It might not be usable everywhere you need an iterator (it
provides no type names), but it's short and sweet.
 
J

John Harrison

The ostream_iterator from the standard library is a template. This
results in a somewhat inelegant syntax. I thought I would share the
following alternative ostream_iterator, which avoids the neccessity for
template instantion

// public domain code by Christopher Diggins

#include <iostream>

struct putter {
putter(const putter& x) : o(x.o), delim(x.delim) { }
putter(std::eek:stream& x = std::cout, const char* s = "")
: o(x), delim(s)
{ }
template<typename T>
putter& operator=(const T& x) {
o << x << delim; return *this;
}
putter& operator*() { return *this; }
putter& operator++() { return *this; }
putter& operator++(int) { return *this; }
mutable std::eek:stream& o;
const char* delim;
};

putter put(std::eek:stream& o = std::cout, const char* delim = "") {
return putter(o, delim);
}

usage is as follows:

int main() {
int array[] = { 1, 2, 4, 8, 16, 32, 64 };
std::copy(array, array + 7, put());
return 0;
}

Hope this is useful.

I like it! Probably wasn't done this way originally because of the lack
of support for member function templates back in the old days.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top