{"id":3325,"date":"2019-01-20T00:42:05","date_gmt":"2019-01-19T22:42:05","guid":{"rendered":"https:\/\/mariusbancila.ro\/blog\/?p=3325"},"modified":"2022-04-11T21:37:08","modified_gmt":"2022-04-11T19:37:08","slug":"cpp-code-samples-before-and-after-ranges","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/","title":{"rendered":"C++ code samples before and after Ranges"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Ranges library proposal has been accepted for C++20 at the San Diego meeting of the standard committee in November last year. The library provides components for handling ranges of values aimed at simplifying our code. Unfortunately, the Ranges library is not very well documented, which makes it harder to grasp for those that want to learn it. This post is intended as an introduction based on examples of code written with and without Ranges.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Eric Niebler&#8217;s implementation of the Ranges library is available <a href=\"https:\/\/github.com\/ericniebler\/range-v3\" rel=\"noopener\" target=\"_blank\">here<\/a>. It works will Clang 3.6.2 or later, gcc 5.2 or later, and VC++ 15.9 or later. The code samples below were written and tested with the latter. On a side note, these samples represent typical implementations and not necessarily the only solutions one could think of.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although the standard namespace for the Ranges library is <tt>std::ranges<\/tt>, in this current implementation of the library it is <tt>ranges::v3<\/tt>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following namespace aliases are used in the samples below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">namespace rs = ranges::v3;\nnamespace rv = ranges::v3::view;\nnamespace ra = ranges::v3::action;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also, for simplicity, we will refer to the following object, function, and lambda functions:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::string to_roman(int value)\n{\n   std::vector&lt;std::pair&lt;int, char const*>> roman\n   {\n      { 1000, \"M\" },{ 900, \"CM\" },\n      { 500, \"D\" },{ 400, \"CD\" },\n      { 100, \"C\" },{ 90, \"XC\" },\n      { 50, \"L\" },{ 40, \"XL\" },\n      { 10, \"X\" },{ 9, \"IX\" },\n      { 5, \"V\" },{ 4, \"IV\" },\n      { 1, \"I\" }\n   };\n\n   std::string result;\n   for (auto const &amp; [d, r]: roman)\n   {\n      while (value >= d)\n      {\n         result += r;\n         value -= d;\n      }\n   }\n\n   return result;\n}\n\nstd::vector&lt;int> v{1,1,2,3,5,8,13,21,34};\n\nauto print_elem = [](auto const e) {std::cout &lt;&lt; e &lt;&lt; '\\n'; };\n\nauto is_even = [](auto const i) {return i % 2 == 0; };<\/pre>\n\n\n\n<div class=\"wp-block-uagb-inline-notice uagb-inline_notice__outer-wrap uagb-inline_notice__align-left uagb-block-dd59f0ea\"><h4 class=\"uagb-notice-title\">UPDATE<\/h4><div class=\"uagb-notice-text\"><p>I would like to thank <strong>Eric Niebler<\/strong> and all the others that commented below with suggestions for these code samples. I have updated a few based on their feedback.<\/p><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Print the all the elements of a range<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::for_each(\n   std::cbegin(v), std::cend(v), \n   print_elem);\n\n\/\/ or \n   \nfor(auto const i : v) \n{\n   print_elem(i); \n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rs::for_each(\n   std::cbegin(v), std::cend(v), \n   print_elem);\n\n\/\/ or\n\nrs::for_each(std::as_const(v), print_elem);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Print all the elements of a range in reverse order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::for_each(\n   std::crbegin(v), std::crend(v),\n   print_elem);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rs::for_each(\n   std::crbegin(v), std::crend(v),\n   print_elem);\n\n\/\/ or\n\nfor (auto const i : v | rv::reverse)\n{\n   print_elem(i);\n};<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Print only the even elements of the range but in reverse order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::for_each(\n   std::crbegin(v), std::crend(v),\n   [print_elem](auto const i) {\n      if(i % 2 == 0)\n         print_elem(i); \n   });<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (auto const i : v \n                  | rv::reverse \n                  | rv::filter(is_even))\n{\n   print_elem(i);\n};<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Skip the first two elements of the range and print only the even numbers of the next three in the range<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">auto it = std::cbegin(v);\nstd::advance(it, 2);\nauto ix = 0;\nwhile (it != std::cend(v) &amp;&amp; ix++ &lt; 3)\n{\n   if (is_even(*it))\n      print_elem(*it);\n   it++;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (auto const i : v \n                  | rv::drop(2)\n                  | rv::take(3)\n                  | rv::filter(is_even))\n{\n   print_elem(i);\n};<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Print all numbers from 101 to 200<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (int n = 101; n &lt;= 200; ++n)\n{\n   print_elem(n);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (auto n : rs::iota_view(101, 201))\n{\n   print_elem(n);\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Print all the Roman numerals from 101 to 200<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To convert a number to its corresponding Roman numeral the function <tt>to_roman()<\/tt> shown ealier is used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (int i = 101; i &lt;= 200; ++i)\n{\n   print_elem(to_roman(i));\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (auto n : rs::iota_view(101, 201) \n            | rv::transform(to_roman))\n{\n   print_elem(n);\n}\n\n\/\/ or\n\nrs::for_each(rv::iota(101, 201), \n             print_element, to_roman);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 <strong>Print the <\/strong>Roman <strong>numeral of the last three numbers divisible to 7 in the range [101, 200], in reverse order<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (int n = 200, count=0; n >= 101 &amp;&amp; count &lt; 3; --n)\n{\n   if (n % 7 == 0)\n   {\n      print_elem(to_roman(n));\n      count++;\n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (auto n : rs::iota_view(101, 201) \n            | rv::reverse\n            | rv::filter([](auto v) {\n                return v % 7 == 0; })\n            | rv::transform(to_roman)\n            | rv::take(3))\n{\n   print_elem(n);\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Create a range of strings containing the Roman numeral of the last three numbers divisible to 7 in the range [101, 200], in reverse order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;std::string> v;\nfor (int n = 200, count = 0; \n     n >= 101 &amp;&amp; count &lt; 3; --n)\n{\n   if (n % 7 == 0)\n   {\n      v.push_back(to_roman(n));\n      count++;\n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">auto v = rs::iota_view(101, 201)\n       | rv::reverse\n       | rv::filter([](auto v) {return v % 7 == 0; })\n       | rv::transform(to_roman)\n       | rv::take(3)\n       | rs::to_vector;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Modify an unsorted range so that it retains only the unique values but in reverse order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;int> v{ 21, 1, 3, 8, 13, 1, 5, 2 };\n\nstd::sort(std::begin(v), std::end(v));\nv.erase(\n   std::unique(std::begin(v), std::end(v)), \n   std::end(v));\nstd::reverse(std::begin(v), std::end(v));<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;int> v{ 21, 1, 3, 8, 13, 1, 5, 2 };\n\nv = std::move(v) | \n    ra::sort | \n    ra::unique | \n    ra::reverse;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Remove the smallest two and the largest two values of a range and retain the other ones, ordered, in a second range<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;int> v{ 21, 1, 3, 8, 13, 1, 5, 2 };\nstd::vector&lt;int> v2 = v;\nstd::sort(std::begin(v2), std::end(v2));\n      \nauto first = std::begin(v2);\nstd::advance(first, 2);\nauto last = first;\nstd::advance(last, std::size(v2) - 4);\n\nv2.erase(last, std::end(v2));\nv2.erase(std::begin(v2), first);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;int> v{ 21, 1, 3, 8, 13, 1, 5, 2 };\nauto v2 = v | \n          rs::copy | \n          ra::sort | \n          ra::slice(2, rs::end - 2);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Concatenate all the strings in a given range into a single value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;std::string> words {\n   \"Lorem\", \" \", \"ipsum\", \" \", \n   \"dolor\", \" \", \"sit\", \" \", \n   \"amet\"};\n\nstd::string text;\nfor (auto const &amp; word : words)\n   text += word;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;std::string> words {\n   \"Lorem\", \" \", \"ipsum\", \" \", \n   \"dolor\", \" \", \"sit\", \" \", \n   \"amet\"};\n\nstd::string text = words | \n                   rs::move | \n                   ra::join;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 Count the number of words (as delimited by space) in a text<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">auto text = \"Lorem ipsum dolor sit amet\";\n\nstd::istringstream iss(text);\nstd::vector&lt;std::string> words(\n   std::istream_iterator&lt;std::string>{iss},\n   std::istream_iterator&lt;std::string>());\nauto count = words.size();\n\n\n\/\/ or\n\nsize_t count = 0;\nstd::vector&lt;std::string> words;\nstd::string token;\nstd::istringstream tokenStream(text);\nwhile (std::getline(tokenStream, token, ' '))\n{\n   ++count;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After ranges:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">auto text = \"Lorem ipsum dolor sit amet\";\n\nauto count = rs::distance(\n   rv::c_str(text) | rv::split(' '));<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Ranges library proposal has been accepted for C++20 at the San Diego meeting of the standard committee in November last year. The library provides components for handling ranges of values aimed at simplifying our code. Unfortunately, the Ranges library is not very well documented, which makes it harder to grasp for those that want to learn it. This post is intended as an introduction based on examples of code written with and without Ranges.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[451,543,138,579],"class_list":["post-3325","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-c20","tag-library","tag-ranges"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,c++20,ranges,library\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"C++ code samples before and after Ranges\" \/>\n\t\t<meta property=\"og:description\" content=\"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-01-19T22:42:05+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2022-04-11T19:37:08+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"C++ code samples before and after Ranges\" \/>\n\t\t<meta name=\"twitter:description\" content=\"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#article\",\"name\":\"C++ code samples before and after Ranges\",\"headline\":\"C++ code samples before and after Ranges\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2019-01-20T00:42:05+02:00\",\"dateModified\":\"2022-04-11T21:37:08+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":16,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#webpage\"},\"articleSection\":\"C++, C++, c++20, library, ranges\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#listItem\",\"name\":\"C++ code samples before and after Ranges\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#listItem\",\"position\":5,\"name\":\"C++ code samples before and after Ranges\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/\",\"name\":\"C++ code samples before and after Ranges\",\"description\":\"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/01\\\/20\\\/cpp-code-samples-before-and-after-ranges\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2019-01-20T00:42:05+02:00\",\"dateModified\":\"2022-04-11T21:37:08+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"C++ code samples before and after Ranges","description":"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/","robots":"max-image-preview:large","keywords":"c++,c++20,ranges,library","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#article","name":"C++ code samples before and after Ranges","headline":"C++ code samples before and after Ranges","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2019-01-20T00:42:05+02:00","dateModified":"2022-04-11T21:37:08+02:00","inLanguage":"en-US","commentCount":16,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#webpage"},"articleSection":"C++, C++, c++20, library, ranges"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#listItem","name":"C++ code samples before and after Ranges"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#listItem","position":5,"name":"C++ code samples before and after Ranges","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/","name":"C++ code samples before and after Ranges","description":"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2019-01-20T00:42:05+02:00","dateModified":"2022-04-11T21:37:08+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"C++ code samples before and after Ranges","og:description":"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.","og:url":"https:\/\/mariusbancila.ro\/blog\/2019\/01\/20\/cpp-code-samples-before-and-after-ranges\/","article:published_time":"2019-01-19T22:42:05+00:00","article:modified_time":"2022-04-11T19:37:08+00:00","twitter:card":"summary","twitter:title":"C++ code samples before and after Ranges","twitter:description":"This article shows a series of code samples for working with ranges with and without the ranges-v3 library."},"aioseo_meta_data":{"post_id":"3325","title":"C++ code samples before and after Ranges","description":"This article shows a series of code samples for working with ranges with and without the ranges-v3 library.","keywords":[{"label":"C++","value":"C++"},{"label":"C++20","value":"C++20"},{"label":"ranges","value":"ranges"},{"label":"library","value":"library"}],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"aioseo-article-63697c74626c3","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:04:15","updated":"2025-12-12 07:51:30","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":16,"uagb_excerpt":"The Ranges library proposal has been accepted for C++20 at the San Diego meeting of the standard committee in November last year. The library provides components for handling ranges of values aimed at simplifying our code. Unfortunately, the Ranges library is not very well documented, which makes it harder to grasp for those that want&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">c++20<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">library<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">ranges<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">c++20<\/span>","<span class=\"advgb-post-tax-term\">library<\/span>","<span class=\"advgb-post-tax-term\">ranges<\/span>"]}},"comment_count":"16","relative_dates":{"created":"Posted 8 years ago","modified":"Updated 4 years ago"},"absolute_dates":{"created":"Posted on January 20, 2019","modified":"Updated on April 11, 2022"},"absolute_dates_time":{"created":"Posted on January 20, 2019 12:42 am","modified":"Updated on April 11, 2022 9:37 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-RD","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/3325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=3325"}],"version-history":[{"count":25,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/3325\/revisions"}],"predecessor-version":[{"id":4491,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/3325\/revisions\/4491"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=3325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=3325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=3325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}