{"id":38,"date":"2011-03-10T09:26:56","date_gmt":"2011-03-10T15:26:56","guid":{"rendered":"http:\/\/til4code.wordpress.com\/?p=16"},"modified":"2011-03-10T09:26:56","modified_gmt":"2011-03-10T15:26:56","slug":"c-and-for-each","status":"publish","type":"post","link":"https:\/\/code.angelmarquez.com\/blog\/2011\/03\/10\/c-and-for-each\/","title":{"rendered":"TIL: About Visual Studio C++ and &#8216;for each in&#8217;"},"content":{"rendered":"<p>Most C++ developers are familiar using with for_each() when iterating over an STL collection. But a few months ago as I read a blog\u00a0<a href=\"http:\/\/www.nuonsoft.com\/blog\/2009\/06\/08\/auto-keyword-in-visual-c-2010\/\">post about the &#8216;auto&#8217; keyword<\/a> over at Marc Gregoire&#8217;s Blog I was dumbstruck when saw the following example:<\/p>\n<p>[sourcecode language=&#8221;cpp&#8221;]<br \/>\nfor each (auto m in myMap)<br \/>\n{<br \/>\n    tcout &lt;&lt; _T(&quot;Map element &quot;) &lt;&lt; m.first &lt;&lt; _T(&quot;: &quot;);<br \/>\n    for each(auto e in m.second)<br \/>\n    {<br \/>\n        tcout &lt;&lt; _T(&quot;&#039;&quot;) &lt;&lt; e &lt;&lt; _T(&quot;&#039;, &quot;);<br \/>\n    }<br \/>\n    tcout &lt;&lt; endl;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>What the heck was this<strong> &#8216;for each&#8217;<\/strong> without an underscore business? Apparently I&#8217;d been painfully <a href=\"http:\/\/blogs.msdn.com\/b\/arich\/archive\/2004\/09\/08\/227139.aspx\">unaware of this new syntax since 2004<\/a>!!! It&#8217;s Microsoft\/Visual Studio specific. But since my primary project is Windows-based through and through, you can bet I&#8217;ll be making use of this beauty to keep my code cleaner.<\/p>\n<p>-CM12<\/p>\n<p>PS. Do note, though, that this syntax automatically dereferences iterators, so it&#8217;s not useful if you need to do iterator manipulation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most C++ developers are familiar using with for_each() when iterating over an STL collection. But a few months ago as I read a blog\u00a0post about the &#8216;auto&#8217; keyword over at Marc Gregoire&#8217;s Blog I was dumbstruck when saw the following example: [sourcecode language=&#8221;cpp&#8221;] for each (auto m in myMap) { tcout &lt;&lt; _T(&quot;Map element &quot;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[32,15,16],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-stl","tag-visual-studio"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":0,"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.angelmarquez.com\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}