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:
[STL Introduction]
Part 1 (sequence containers)
Part 2 (associative containers)
Part 3 (smart pointers)
Part 4 (Nurikabe solver) – see Wikipedia’s article and my updated source code
Part 5 (Nurikabe solver, continued)
Part 6 (algorithms and functors)
Part 7 (algorithms and functors, continued)
Part 8 (regular expressions)
Part 9 (rvalue references)
Part 10 (type traits)
[Advanced STL]
Part 1 (shared_ptr – type erasure)
Part 2 (equal()/copy() – algorithm optimizations)
Part 3 (_ITERATOR_DEBUG_LEVEL, #pragma detect_mismatch, and /d1reportSingleClassLayout)
Part 4 (rvalue references v2.1 and associative container mischief)
Part 5 (deduplicator, using Boost.Bimap/Filesystem/ScopeExit) – see my deduplicate.cpp
Part 6 (container pretty printer) – see my pretty_printer.cpp
Stephan T. Lavavej
Visual C++ Libraries Developer
0 comments