Tech Off Post

Single Post Permalink

View Thread: Templated STL container pretty printer
  • User profile image

    [Preface: I just discovered and watched STL's series "STL" and "Advanced STL". In response to STL#2's homework question to create a template to erase predicated elements from a container, I'd like to post a similar "homework" question.]

    Task: Write a templated solution that will pretty-print the contents of any STL container, or more generally anything that supports begin/end iterators.

    I originally posted this question on StackOverflow (, but looking back at it I'm still not happy with any of the proposed solutions.

    Synopsis: Given something like std::CONTAINER x, I want to write "std::cout << x;" and get something like "{1, 5, -3, 12}" (with customizable delimiters), but I don't want to have to change the code to accomodate new containers -- I'd like a single piece of code that will work for ALL containers that have begin/end.

    Any suggestions (also to the original SO page) would be most welcome!