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: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:stream& o;
const char* delim;
};
putter put(std: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
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: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:stream& o;
const char* delim;
};
putter put(std: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