Re: [boost] io operations for stl containers?
From: Vladimir Prus [EMAIL PROTECTED] Terje Slettebø wrote: From: Vladimir Prus [EMAIL PROTECTED] after having to output std::vector to stream again and again using custom 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 for 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 hard-coded brackets and separators. One might use an xalloc() value for each type to 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 a persistent template library for C++ containers some time back. It is probably on the DDJ web site, although I haven't looked there recently. You might want to check that out and see what he did. I will readily admit I have not had the need to persist container data in my daily programming but 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 way. One might call this output operators are mostly debugging help, but why not have standard debugging help? std::cout boost::io_formatMap(,, [, ]) boost::io_formatMapList(,,\n) list '\n'; This might print: [1, a] [2, b] [3, c] This example should one case where manipulators are desirable: vector vectorint 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 container to go on separate line. The above io_format's are intended to be manipulators. You could get this manipulator with: io_formatvectorint (\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 per-type basis. 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: int main() { typedef std::pairint,char Map; typedef std::vectorMap MapList; MapList list; list.push_back(std::make_pair(1,'A')); list.push_back(std::make_pair(2,'B')); list.push_back(std::make_pair(3,'C')); std::cout io_formatMap([,],,) io_formatMapList(,,\n) list '\n'; } Output: [1,A] [2,B] [3,C] 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 welcome. Regards, Terje Test.cpp Description: Binary data ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] io operations for stl containers?
At 03:22 AM 2/4/2003, Vladimir Prus wrote: Terje Slettebø wrote: ... Have you looked at Jen Maurer's persistence library? It was an elegant design and quite good at handling the issues you are discussing, IIRC. It is still in CVS under the branch persistence-initial. I've always been sorry Jens lost interest in carrying it forward to formal review. --Beman ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
[boost] io operations for stl containers?
Hi, after having to output std::vector to stream again and again using custom 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? I was thinking about boost/io/vector.hpp boost/io/set.hpp and so on. There are basically two approaches: 1. Operators use fixed format: bracked list with commas between values for 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. Thoughts? - Volodya ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] io operations for stl containers?
From: Terje Slettebø [EMAIL PROTECTED] From: Vladimir Prus [EMAIL PROTECTED] after having to output std::vector to stream again and again using custom 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? I was thinking about boost/io/vector.hpp boost/io/set.hpp and so on. [...] You can do this quite well using the standard library and stream iterator adapters. This may do both of your approaches above. For example: [...] typedef std::ostream_iteratorstd::string Out; std::cout Print vector\n; std::copy(list.begin(),list.end(),Out(std::cout,\n)); Now try the same with std::map std::string, std::vector std::vectorint . With op, it's still one statement. ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] io operations for stl containers?
From: Vladimir Prus [EMAIL PROTECTED] after having to output std::vector to stream again and again using custom 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? I was thinking about boost/io/vector.hpp boost/io/set.hpp and so on. There are basically two approaches: 1. Operators use fixed format: bracked list with commas between values for 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 hard-coded brackets and separators. One might use an xalloc() value for each type to output. For example something like: typedef std::pairint,char Map; typedef std::vectorMap MapList; MapList list; // Fill container std::cout boost::io_formatMap(,, [, ]) boost::io_formatMapList(,,\n) list '\n'; This might print: [1, a] [2, b] [3, c] Regards, Terje ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] io operations for stl containers?
From: Glenn G. Chappell [EMAIL PROTECTED] Vladimir Prus wrote: after having to output std::vector to stream again and again using custom 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? ... std::cout new path is v \n; The philosophy behind the STL would suggest accessing containers through their iterators. Something like std::cout new path is Foo(v.begin(), v.end()) \n; would not be too bad (and it would be even better if it were called something besides Foo). This would be very general, while remaining quite readble, I think. Delimiters could be set using optional parameters, among other methods. std::cout new path is Foo(v.begin(), v.end(), [, ,, ]) \n; You then still have the issue of what to do with the element types. For example: std::vectorstd::pairchar,int v; std::cout Foo(v.begin(),v.end(),[,,,]) '\n'; How to print each element, and what if you want different delimiters for the elements? Regards, Terje ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost