473,675 Members | 3,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sharing a Generic Ostream Iterator

The ostream_iterato r from the standard library is a template. This
results in a somewhat inelegant syntax. I thought I would share the
following alternative ostream_iterato r, 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::ost ream& x = std::cout, const char* s = "")
: o(x), delim(s)
{ }
template<typena me 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::ostrea m& 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 1822
On 2005-11-11, cd******@videot ron.ca <cd******@video tron.ca> wrote:
The ostream_iterato r from the standard library is a template. This
results in a somewhat inelegant syntax. I thought I would share the
following alternative ostream_iterato r, 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::ost ream& x = std::cout, const char* s = "")
: o(x), delim(s)
{ }
template<typena me 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::ostrea m& 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******@videot ron.ca wrote:
The ostream_iterato r from the standard library is a template. This
results in a somewhat inelegant syntax. I thought I would share the
following alternative ostream_iterato r, 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::ost ream& x = std::cout, const char* s = "")
: o(x), delim(s)
{ }
template<typena me 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::ostrea m& 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
2346
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'; cout << get_nth(v, 2) << '\n'; } that prints the second and third (counting from zero) items of given
4
4423
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 reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this possible? If yes, can anyone show me simple examples in C++
19
2243
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); doesn't work because I need the container to write: container.erase(p), where p is an iterator.
2
2952
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
1975
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 "transform" which works not only on lists, but lists of list, lists of lists of lists, etc. I'm calling it "fmap" and it passes around actual lists instead of iterators (for now). In order to get it to work, I thought I'd have one templated...
3
3628
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 from object to string frequently). Basically generics and not its non- generic counterpart. string str1 = "abc: value1 def: value2 ghi: value3"; char delimiterChars = { '\t' }; string tokens = str1.Split(delimiterChars);
8
1934
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 following output on the console Upper 0002A634lower 0002A634
2
1670
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 the same time. More specifically, the main advantage of the single_pass_search over the std::search is its capability to search for a sub-sequence in a search-range that is accessed through a pair of single-pass (input) iterators, while...
2
2550
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 != VV.end(); ++it ) { out << (*it); } return out << std::endl;
0
8967
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8719
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7499
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5754
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4259
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2865
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 we have to send another system
2
2120
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1863
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.