From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-02-04 04:46:40
>From: "Vladimir Prus" <ghost_at_[hidden]>
> Terje Slettebø wrote:
> >>From: "Vladimir Prus" <ghost_at_[hidden]>
> >>after having to output std::vector to stream again and again using
> >>solution, I started to wonder why we don't have a solution in boost.
> >>Does it makes sense to include operators<< for vectors, sets, etc?
> >>and so on. There are basically two approaches:
> >>1. Operators use fixed format: bracked list with commas between values
> >> vector, for example.
> >>2. Manipulators are provided to set brackets and separators.
> >>I had implemented the second approach some time ago, but it turned out
> >> that was overkill. So, 1) looks better now.
> > If this is done as a library, then I think it's best not to have
> > brackets and separators. One might use an xalloc() value for each type
> > output. For example something like:
> You see, that's what I have implemented back in 2001. You could change
> braces/separator for each stl container, and it used xalloc()/pword().
> There's one problem though: I don't remember the syntax of brace
> changing, just because I used it a couple of times long ago and then
> stopped. Probably, the scope should be more clearly defined:
> Edward Diener wrote:
> > Al Stevens who writes the C++ column for "Doctor Dobbs Journal" put out
> > persistent template library for C++ containers some time back. It is
> > probably on the DDJ web site, although I haven't looked there recently.
> > might want to check that out and see what he did. I will readily admit
> > have not had the need to persist container data in my daily programming
> > I can understand others having that need.
> Rozental, Gennadiy wrote:
> > I do not see a way how you could implement solution with better
> > flexibility/conciseness ratio than copy+ostream_iterator one.
> I'm not much interested in persistence (after all, I hope that Robert's
> library will take care of that). Likewise, I never needed arbitrary
> delimiters, etc. But while developing an algorithm for finding k shortest
> paths in a graph, I need to output each path, and have no easy standard
> One might call this output operators are mostly debugging help, but why
> have standard debugging help?
> > std::cout << boost::io_format<Map>(",", "[", "]") <<
> > boost::io_format<MapList>("","","\n") << list << '\n';
> > This might print:
> > [1, a]
> > [2, b]
> > [3, c]
> This example should one case where manipulators are desirable:
> vector< vector<int> > v;
> cout << v ;
> Here, each nested vector better go on a separate line. I suggest:
> cout << multiline << v;
> where "multiline" manipulator causes each element of the next output
> to go on separate line.
The above io_format<>'s are intended to be manipulators. You could get this
io_format<vector<int> >("\n","","") multiline;
You could also make it so that this manipulator set the format for any
container, but in cases where you have arbitrary deep nesting of containers
(like in Peter Dimov's posting), it may be better to set the format on a
Incidentally, I've just made a version that does exactly this. :) I've
attached it, including a test, with this posting.
It's used like this:
typedef std::pair<int,char> Map;
typedef std::vector<Map> MapList;
std::cout << io_format<Map>("[","]",",") << io_format<MapList>("","","\n")
<< list << '\n';
It's a little rough, as it doesn't do proper stream error handling, or
ownership management for the pword object, but it works. Feedback is
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk