1

I would like to output list of strings value to ostream.

I can declare and implement overloading function for this:

ostream& operator<< (ostream &out, const list<string> &in);

... and then write like

cout << value;

... but there are at least two possible ways to dump list of strings: one string per line or all strings in one line separated by spaces (or maybe other separator).

Is it possible to change dumping function behavior through stream controlling?

I would like to write something like:

list<string> lst;
...
cout << print_as_multiline() << lst;

... and:

list<string> lst;
...
cout << print_as_one_line() << set_separator (", ") << lst;
  • 4
    Why not just `std::copy`? `std::ostream_iterator` lets you control what's between each of them. – chris Jan 09 '13 at 09:58
  • As an attempt of better readability –  Jan 09 '13 at 09:59
  • string is null terminated, to print_as_one_line, you need to remove `\n` – billz Jan 09 '13 at 10:00
  • Fine, then move it to a function that accepts a string to pass to it. – chris Jan 09 '13 at 10:00
  • 3
    The [pretty printer](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers) supports custom delimiters. It might be worth a shot. – Kerrek SB Jan 09 '13 at 10:02
  • 1
    IMO, any attempt to implement such an approach will be overly complicated, simpler to have two functions, `print_multi()`, `print_single()`, which takes the list and the stream and simply iterates through and prints it out... code is clear and intention is clear... – Nim Jan 09 '13 at 10:02

2 Answers2

0

I have an idea… could you test it? In your operator << implementation file Create a global,static?? variable string Separator= def sep (", ").

Use it in your <<, (and before return reset to def sep?- your decision)

Create a class set_separator, with the implementation of the constructor in the same file as your <<. In this constructor set Separator to the argument.

Define a new operator << for class set_separator, with do nothing, just return the stream.

Similar with multiline output.

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
0

There are several options for that: - Write multiple functions or a function taking a separator as parameter. - Attach the separator to the stream. You can use the iostream's xalloc() function to allocate a custom property slot (it should be allocated only once and then applies to all streams). You can then use the streams' iword() and pword() memberfunctions to access the attached info. There is also an event that you can hook into, so a custom function is called when e.g. the stream is destroyed. Use that in order to release dynamically allocated content, in case that is necessary. - Create a so-called facet that takes care of the list formatting. This facet is attached to the stream's locale.

My advise: Take the first option, it's the least complicated and most straightforward one. If you need to decide the formatting in one place but actually use it in a completely different place, use the second one. Read Langer&Kreft C++ IOStreams and Locales before you consider the third option. ;)

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55