By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

Sharing a Generic Ostream Iterator

P: n/a
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::ostream& 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::ostream& o;
const char* delim;
};

putter put(std::ostream& 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

Nov 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2005-11-11, cd******@videotron.ca <cd******@videotron.ca> wrote:
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::ostream& 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::ostream& o;
const char* delim;
};
Member o needn't be mutable, need it?
putter put(std::ostream& 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.

--
Neil Cerutti
Nov 11 '05 #2

P: n/a
Thanks for pointing out the flaws Neil!

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

Nov 11 '05 #3

P: n/a
cd******@videotron.ca wrote:
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::ostream& 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::ostream& o;
const char* delim;
};

putter put(std::ostream& 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
Nov 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.