{"id":1890,"date":"2013-07-09T20:10:00","date_gmt":"2013-07-10T01:10:00","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1890"},"modified":"2013-07-09T20:10:00","modified_gmt":"2013-07-10T01:10:00","slug":"modern-c-iterators-and-loops-compared-to-c","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1890","title":{"rendered":"Modern C++, iterators and loops compared to C#"},"content":{"rendered":"

It has been a while since I looked much at modern C++. I\u2019ve started intentionally simple and was exploring some of the ways that a simple list can be iterated. I hate to say this, but I remember the days when the standard template library was not necessarily something that was the easiest to get working on Windows with a Microsoft C++ compiler in a simple, safe and reliable way. My, how times have changed for the better!<\/p>\n

While the overall syntax of C++ lacks the refined and designed elegance of a C# application, it has come a very long way!<\/p>\n

I\u2019ve thrown together a simple sample. The code creates a list of strings, pushes (not add<\/strong>, arrgh!) strings onto the list, and then iterates through the list in three similar, but syntactically different methods below:<\/p>\n

\"image\"<\/a><\/p>\n

OK, there\u2019s a few awesome things here that make C++ much more approachable these days: <\/p>\n

    \n
  1. a decent string<\/strong> class! No more hunting for a library or header file that has most of the functionality you need. (there\u2019s a wstring as well for wchar_t needs)<\/li>\n
  2. All the common Data structure types like list<\/strong>, map<\/strong>, vector<\/strong>, etc\u2026 that you might need without writing them your self.<\/li>\n
  3. With using<\/strong>, the code doesn\u2019t need to refer to types with namespaces (much like C#). So, instead of std::list<std:string><\/strong>, it\u2019s just list<string><\/strong>, which I consider far more readable at a glance.<\/li>\n
  4. No weird casting or worrying about strings when values are passed to the push_back function. Again, it just works.<\/li>\n
  5. auto<\/strong> \u2013 the var<\/strong> of C# is called auto <\/strong>in C++. If I hadn\u2019t used auto<\/strong> in the first iterator, the code would have declared the type as list<string>::iterator<\/strong>. While not terrible, auto <\/strong>reads well, as it\u2019s likely that I\u2019ll not need the detailed declaration to understand what\u2019s being iterated upon. <\/li>\n
  6. Second option is just syntactical sugar as it internally uses a standard for<\/strong> loop like in the first example internally. But, you\u2019ll note there are anonymous functions\/lambdas! The square [] brackets is the capture clause of the lambda expression. Unlike C#, where the compiler automatically determines what outer scoped variables will be used by the inner lambda function, in C++, it\u2019s necessary to explicitly declare what is required. While this is a bit annoying, there are times where this extra declaration might cause programmers to think twice about all of the variables that are required in the lambda expression. In this instance, there aren\u2019t any variables needed, so it\u2019s empty.<\/li>\n
  7. The last example is the most concise, and maybe a little less friendly at first and that\u2019s mostly due to the heritage of C++ and how to make code most efficient. It\u2019s called a range-based for statement<\/a>. First, the code is using auto<\/strong> again and the type is a string<\/strong> as it\u2019s declaring the type used within the list. The & symbol remember is a reference and the const<\/strong> is indicating that the value will not be changed by the inner block. These two together ideally make it so the value is observed in-place, without any need for a copy. <\/li>\n<\/ol>\n

    While the range-based for <\/strong>statement isn\u2019t quite as readable as C#:<\/p>\n

    \"image\"<\/a><\/p>\n

    I think you might agree that the C++ version is more than passable, and nearly friendly! <\/p>\n

    \"image\"<\/a><\/p>\n

    (Aside, there is a shortcut initializer list syntax that can be used with non-aggregate types in C++, so an int <\/strong>for example that would have gotten rid of the calls to push_back<\/strong>).<\/p>\n","protected":false},"excerpt":{"rendered":"

    It has been a while since I looked much at modern C++. I\u2019ve started intentionally simple and was exploring some of the ways that a simple list can be iterated. I hate to say this, but I remember the days when the standard template library was not necessarily something that was the easiest to get […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4],"tags":[82],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-uu","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1705,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1705","url_meta":{"origin":1890,"position":0},"title":"WinRT\/Xaml\/AKA Metro DataTemplate selection based on Data Types","date":"August 20, 2012","format":false,"excerpt":"You may have noticed that WinRT does not have automatic resolution of a DataTemplate based on the data type of object added to an ItemsControl. While unfortunate as this behavior is quite handy, it\u2019s not too difficult to replicate the functionality using a DataTemplateSelector. WPF for example, could do something\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1835,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1835","url_meta":{"origin":1890,"position":1},"title":"How to rewrite a MongoDB C# LINQ with a Projection Requirement using a MongoCursor","date":"January 26, 2013","format":false,"excerpt":"The LINQ Provider for MongoDB does not currently take into account data projections efficiently when returning data. This could mean that you\u2019re unnecessarily returning more data from the database than is needed. So, I\u2019m going to show you the pattern I applied as a replacement for the LINQ queries when\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1442,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1442","url_meta":{"origin":1890,"position":2},"title":"Nest Thermostat API\/Protocol","date":"January 8, 2012","format":false,"excerpt":"While Nest Labs hasn\u2019t released a formal (documented & supported) API, I thought I\u2019d do a bit of digging to see how they\u2019re using the network and what might be achievable. A few things are going on, the majority as you\u2019d probably expect. The web interface is using a long\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/01\/image_thumb7.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1329,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1329","url_meta":{"origin":1890,"position":3},"title":"My Nest Thermostat installation experience","date":"December 18, 2011","format":false,"excerpt":"After the amazing mad dash for the Nest thermostats when they were first made available for pre-order, I ordered three thermostats for our home from Best Buy (as Nest.com had sold out). We\u2019ve got a three zone heating system, and I wanted to replace all at once (as the system\u2026","rel":"","context":"In "Recommendations"","img":{"alt_text":"20111218-IMG_0096","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2011\/12\/20111218-IMG_0096_thumb.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":568,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/568","url_meta":{"origin":1890,"position":4},"title":"Data Binding and Tooltips in Silverlight","date":"September 19, 2008","format":false,"excerpt":"Have you ever wanted to databind a tooltip in Silverlight (or WPF for that matter), and found that the DataContext isn't available for tooltips (the datacontext is null)? It's very annoying. Tooltips, unfortunately, aren't connected to their parents in anyway when they're created, so they loose the ability to connect\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1730,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1730","url_meta":{"origin":1890,"position":5},"title":"How to find an element in a DataTemplate in WinRT\/XAML.","date":"September 7, 2012","format":false,"excerpt":"Here\u2019s one way to find a named element in a DataTemplate in XAML in Windows 8 XAML. You might try FindName to discover it doesn\u2019t work. That\u2019s because it\u2019s not recursive. So, I created a simple extension method to do the same thing: public static class FrameworkElementExtensions { public static\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/09\/image.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1890"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=1890"}],"version-history":[{"count":0,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1890\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=1890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=1890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=1890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}