{"id":2853,"date":"2011-07-11T23:46:00","date_gmt":"2011-07-11T23:46:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2011\/07\/11\/container-pretty-printer-advanced-stl-part-6\/"},"modified":"2019-02-18T18:44:50","modified_gmt":"2019-02-18T18:44:50","slug":"container-pretty-printer-advanced-stl-part-6","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/container-pretty-printer-advanced-stl-part-6\/","title":{"rendered":"Container Pretty Printer &#8211; Advanced STL, Part 6"},"content":{"rendered":"<p><span style=\"font-family: verdana,geneva;font-size: medium\">In <a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-6-of-n\"><span style=\"color: #0000ff\">Part 6<\/span><\/a> of my second video lecture series focused on the Standard Template Library, I demonstrate how to write a &#8220;pretty printer&#8221; for STL containers that&#8217;s capable of formatting a vector&lt;tuple&lt;int, string, int&gt;&gt; as [(1, &#8220;ten&#8221;, 100), (2, &#8220;twenty&#8221;, 200), (3, &#8220;thirty&#8221;, 300)].&nbsp; It&#8217;s easy to use, with the default syntax being print(cout, container), or print_line(cout, container) to emit a newline.&nbsp; It&#8217;s also highly customizable with just a bit of extra code.&nbsp; Following the Visual Studio IDE&#8217;s visualizers, which display a container&#8217;s size before its elements, I show how to provide a custom formatter that can print a set&lt;pair&lt;int, int&gt;&gt; as [3]{(11, 22), (33, 44), (55, 66)}.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\">This advanced series assumes that you&#8217;re familiar with C++ and the STL&#8217;s interface, but not the STL&#8217;s implementation.&nbsp; If you haven&#8217;t used the STL extensively yet, I recommend watching my introductory series.&nbsp; For reference, here are all of the links:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\">[STL Introduction]<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/shows\/Going+Deep\/C9-Lectures-Introduction-to-STL-with-Stephan-T-Lavavej\/\"><span style=\"color: #0000ff\">Part 1<\/span><\/a> (sequence containers)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-2-of-n\/\"><span style=\"color: #0000ff\">Part 2<\/span><\/a> (associative containers)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-3-of-n\/\"><span style=\"color: #0000ff\">Part 3<\/span><\/a> (smart pointers)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-4-of-n\/\"><span style=\"color: #0000ff\">Part 4<\/span><\/a> (Nurikabe solver) &#8211; see Wikipedia&#8217;s <a href=\"http:\/\/en.wikipedia.org\/wiki\/Nurikabe\"><span style=\"color: #0000ff\">article<\/span><\/a> and my <a href=\"https:\/\/skydrive.live.com\/redir.aspx?cid=e66e02dc83efb165&amp;resid=E66E02DC83EFB165!120\"><span style=\"color: #0000ff\">updated source code<\/span><\/a><\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-5-of-n\/\"><span style=\"color: #0000ff\">Part 5<\/span><\/a> (Nurikabe solver, continued)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-6-of-n\"><span style=\"color: #0000ff\">Part 6<\/span><\/a> (algorithms and functors)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-7-of-n\"><span style=\"color: #0000ff\">Part 7<\/span><\/a> (algorithms and functors, continued)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-8-of-n\"><span style=\"color: #0000ff\">Part 8<\/span><\/a> (regular expressions)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-9-of-n\"><span style=\"color: #0000ff\">Part 9<\/span><\/a> (rvalue references)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-10-of-10\"><span style=\"color: #0000ff\">Part 10<\/span><\/a> (type traits)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\">[Advanced STL]<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-1-of-n\"><span style=\"color: #0000ff\">Part 1<\/span><\/a> (shared_ptr &#8211; type erasure)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-2-of-n\"><span style=\"color: #0000ff\">Part 2<\/span><\/a> (equal()\/copy() &#8211; algorithm optimizations)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-3-of-n\"><span style=\"color: #0000ff\">Part 3<\/span><\/a> (_ITERATOR_DEBUG_LEVEL, #pragma detect_mismatch, and \/d1reportSingleClassLayout)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-4-of-n\"><span style=\"color: #0000ff\">Part 4<\/span><\/a> (rvalue references v2.1 and associative container mischief)<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-5-of-n\"><span style=\"color: #0000ff\">Part 5<\/span><\/a> (deduplicator, using Boost.<a href=\"http:\/\/www.boost.org\/doc\/libs\/release\/libs\/bimap\/doc\/html\/index.html\"><span style=\"color: #0000ff\">Bimap<\/span><\/a>\/<a href=\"http:\/\/www.boost.org\/doc\/libs\/release\/libs\/filesystem\/index.html\"><span style=\"color: #0000ff\">Filesystem<\/span><\/a>\/<a href=\"http:\/\/www.boost.org\/doc\/libs\/release\/libs\/scope_exit\/doc\/html\/index.html\"><span style=\"color: #0000ff\">ScopeExit<\/span><\/a>) &#8211; see my <a href=\"https:\/\/skydrive.live.com\/redir.aspx?cid=e66e02dc83efb165&amp;resid=E66E02DC83EFB165!290\"><span style=\"color: #0000ff\">deduplicate.cpp<\/span><\/a><\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\"><a href=\"https:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-6-of-n\"><span style=\"color: #0000ff\">Part 6<\/span><\/a> (container pretty printer) &#8211; see my <a href=\"https:\/\/skydrive.live.com\/redir.aspx?cid=e66e02dc83efb165&amp;resid=E66E02DC83EFB165!292\"><span style=\"color: #0000ff\">pretty_printer.cpp<\/span><\/a><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\">Stephan T. Lavavej<\/span><\/p>\n<p><span style=\"font-family: verdana,geneva;font-size: medium\">Visual C++ Libraries Developer<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part 6 of my second video lecture series focused on the Standard Template Library, I demonstrate how to write a &#8220;pretty printer&#8221; for STL containers that&#8217;s capable of formatting a vector&lt;tuple&lt;int, string, int&gt;&gt; as [(1, &#8220;ten&#8221;, 100), (2, &#8220;twenty&#8221;, 200), (3, &#8220;thirty&#8221;, 300)].&nbsp; It&#8217;s easy to use, with the default syntax being print(cout, container), [&hellip;]<\/p>\n","protected":false},"author":289,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[9,3,32],"class_list":["post-2853","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-channel-9","tag-dev","tag-stl"],"acf":[],"blog_post_summary":"<p>In Part 6 of my second video lecture series focused on the Standard Template Library, I demonstrate how to write a &#8220;pretty printer&#8221; for STL containers that&#8217;s capable of formatting a vector&lt;tuple&lt;int, string, int&gt;&gt; as [(1, &#8220;ten&#8221;, 100), (2, &#8220;twenty&#8221;, 200), (3, &#8220;thirty&#8221;, 300)].&nbsp; It&#8217;s easy to use, with the default syntax being print(cout, container), [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/2853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/289"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=2853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/2853\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=2853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=2853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=2853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}