473,467 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sharing a Generic Ostream Iterator

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
3 1814
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
Thanks for pointing out the flaws Neil!

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

Nov 11 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlo Milanesi | last post by:
Let's say I want to write the following function, void f(const list<int> &l, const vector<int> &v) { cout << get_second(l) << '\n'; cout << get_second(v) << '\n'; cout << get_nth(l, 2) << '\n';...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
19
by: Nafai | last post by:
Hi I want to write a function which erases al the repeated elements in a range. How should be the prototype? template <class Iterator> void eraseRepeated(Iterator begin, Iterator end); ...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
3
by: Dave | last post by:
I'm calling string.Split() producing output string. I need direct access to its enumerator, but would greatly prefer an enumerator strings and not object types (as my parsing is unsafe casting...
8
by: dev_15 | last post by:
Hi, i have the following program to display the input received separated into any word that has Uppercase letters and all lowercase words. For the following input "Upper lower" i get the...
2
by: jimxoch | last post by:
Dear list, I have recently implemented a generic sequence searching template function, named single_pass_search, which is more generic than std::search and has better worst case complexity at...
2
by: Hansel Stroem | last post by:
Is this legitimate STL code ? Compiler seems not to like the iterator ... template <typename Tn> ostream& operator<<(ostream& out, vector<TnVV) { for ( vector<Tn>::iterator it = VV.begin(); it...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.