Container Pretty Printer – Advanced STL, Part 6

In Part 6 of my second video lecture series focused on the Standard Template Library, I demonstrate how to write a “pretty printer” for STL containers that’s capable of formatting a vector<tuple<int, string, int>> as [(1, “ten”, 100), (2, “twenty”, 200), (3, “thirty”, 300)]. It’s easy to use, with the default syntax being print(cout, container), or print_line(cout, container) to emit a newline. It’s also highly customizable with just a bit of extra code. Following the Visual Studio IDE’s visualizers, which display a container’s size before its elements, I show how to provide a custom formatter that can print a set<pair<int, int>> as [3]{(11, 22), (33, 44), (55, 66)}.

This advanced series assumes that you’re familiar with C++ and the STL’s interface, but not the STL’s implementation. If you haven’t used the STL extensively yet, I recommend watching my introductory series. For reference, here are all of the links: