{"id":4599,"date":"2023-01-16T21:25:24","date_gmt":"2023-01-16T19:25:24","guid":{"rendered":"https:\/\/mariusbancila.ro\/blog\/?p=4599"},"modified":"2023-01-16T21:25:27","modified_gmt":"2023-01-16T19:25:27","slug":"how-to-make-chunks-of-a-range-in-cpp23","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/","title":{"rendered":"How to make chunks of a range in C++23"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Last year, I wrote a <a href=\"https:\/\/mariusbancila.ro\/blog\/2022\/03\/16\/new-cpp23-range-adaptors\/\" target=\"_blank\" rel=\"noopener\" title=\"\">blog post<\/a> about new C++23 range adaptors for joining and zipping. The C++23 standard includes a longer lists of range adapters (you can find a list <a href=\"https:\/\/mariusbancila.ro\/blog\/2022\/12\/23\/the-cpp23-standard-break-down\/\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>). Among them, there are several adaptors used for creating views consisting of chunks or &#8220;windows&#8221; of the elements of a given range. In this blog post, I will present these adaptors.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">adjacent<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::ranges::adjacent_view<\/code> is a range adaptor that takes a view and produces a new view whose elements are windows of <code>N<\/code> adjacent elements of the original view. To understand this better, let&#8217;s consider we have a view consisting of the following elements: <code>[1, 2, 3, 4, 5, 6, 7, 8]<\/code>. You need to process these elements in chunks of 3 consecutive elements, but in a sliding manner: <code>[1, 2, 3]<\/code>, <code>[2, 3, 4]<\/code>, etc. up to <code>[6, 7, 8]<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_adjacent_view.png\"><img loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"359\" src=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_adjacent_view.png\" alt=\"\" class=\"wp-image-4606\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_adjacent_view.png 408w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_adjacent_view-300x264.png 300w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can do this as following:<\/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 v = { 1, 2, 3, 4, 5, 6, 7, 8 };\nfor (auto const&amp; t : v | std::views::adjacent&lt;3>)\n{\n   std::cout &lt;&lt; '[' &lt;&lt; std::get&lt;0>(t) &lt;&lt; ',' \n                    &lt;&lt; std::get&lt;1>(t) &lt;&lt; ',' \n                    &lt;&lt; std::get&lt;2>(t) \n             &lt;&lt; ']' &lt;&lt; '\\n';\n} <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This prints the following to the output console:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[1,2,3]\n[2,3,4]\n[3,4,5]\n[4,5,6]\n[5,6,7]\n[6,7,8]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since the elements of the resulting view are tuples of references to elements of the original view, we can use structure binding to decompose the tuple and access it elements easier:<\/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&amp; [a,b,c] : v | std::views::adjacent&lt;3>)\n{\n   std::cout &lt;&lt; '[' &lt;&lt; a &lt;&lt; ',' &lt;&lt; b &lt;&lt; ',' &lt;&lt; c &lt;&lt; ']' &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the number of elements of each chunk (window) is 2, then you can use the <code>pairwise<\/code> view, which is defined as follows:<\/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 views {\n    inline constexpr auto pairwise = adjacent&lt;2>;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (auto const&amp; [a,b] : v | std::views::pairwise)\n{\n   std::cout &lt;&lt; '[' &lt;&lt; a &lt;&lt; ',' &lt;&lt; b &lt;&lt; ']' &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>adjacent_view<\/code> adaptor requires that the size of the chunks is provided at compile-time, which means it cannot be used if you only know it at runtime. However, another adaptor allows you to do just that.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">slide<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::ranges::slide_view<\/code> is a range adaptor that works just like <code>adjacent_view<\/code>, except that the chunk size is provided at runtime. Also, the elements of the result view are not tuples but other views.<\/p>\n\n\n\n<div class=\"wp-block-uagb-image uagb-block-9c44de9c wp-block-uagb-image--layout-default wp-block-uagb-image--effect-static wp-block-uagb-image--align-none\"><figure class=\"wp-block-uagb-image__figure\"><a class=\"\" href=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_slide_view-1.png\" target=\"\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_slide_view-1.png\" alt=\"\" class=\"uag-image-4607\" title=\"\" loading=\"lazy\" \/><\/a><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"> Here is an example:<\/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 v = { 1, 2, 3, 4, 5, 6, 7, 8 };\nfor (auto const&amp; c : v | std::views::slide(3))\n{\n   print(c);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet will produce the same output we saw earlier:<\/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=\"\">[1,2,3]\n[2,3,4]\n[3,4,5]\n[4,5,6]\n[5,6,7]\n[6,7,8]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For clarity, here is the implementation of the <code>print()<\/code> function used in this snippet:<\/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=\"\">template &lt;typename R>\nauto print(R&amp;&amp; r)\n{\n   std::cout &lt;&lt; '[';\n   bool first = true;\n   for (auto const e : r)\n   {\n      if(first) first = false;\n      else std::cout &lt;&lt; ',';\n       \n      std::cout &lt;&lt; e;\n   }\n   std::cout &lt;&lt; ']' &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">chunk<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you need to split the original view in chunks of <code>N<\/code> elements (and not produce chunks by &#8220;sliding&#8221; along the original view) you can use the <code>std::ranges::chunk_view<\/code> adaptor. The size of the chunk is a runtime value, just as for <code>std::ranges::slide_view<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-uagb-image uagb-block-54d0d83f wp-block-uagb-image--layout-default wp-block-uagb-image--effect-static wp-block-uagb-image--align-none\"><figure class=\"wp-block-uagb-image__figure\"><a class=\"\" href=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_chunk_view-1.png\" target=\"\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_chunk_view-1.png\" alt=\"\" class=\"uag-image-4608\" title=\"\" loading=\"lazy\" \/><\/a><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example:<\/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 v = { 1, 2, 3, 4, 5, 6, 7, 8 };\nfor (auto const&amp; c : v | std::views::chunk(3))\n{\n   print(c);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will produce the following output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[1,2,3]\n[4,5,6]\n[7,8]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In practice, you might need to make this splitting not using a fixed size, but by some specific criteria. In this case, another range adaptor is available.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">chunk_by<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>std::ranges::chunk_by_view<\/code> range adaptor is similar to <code>std::ranges::chunk_view<\/code> except that instead of providing a size, you provide a binary predicate. The splitting occurs between each pair of adjacent elements for which the predicate returns <code>false<\/code>. Let&#8217;s take a couple of examples to understand it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the first example, we want to split a view into multiple chunks so that each chunk contains elements so that each adjacent pair does not differ by more than 1.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_chunk_by_view.png\"><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"252\" src=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_chunk_by_view.png\" alt=\"\" class=\"wp-image-4609\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_chunk_by_view.png 573w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_chunk_by_view-300x132.png 300w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/a><\/figure>\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=\"\">bool differ_by_one(int const a, int const b)\n{\n    return std::abs(a - b) &lt;= 1;\n}\n\nstd::vector v = {1,1,2,3,2,2,1,3,4,8,7,6,7};\nfor (auto const&amp; c : v | std::views::chunk_by(differ_by_one))\n{\n   print(c);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The predicate used here is <code>differ_by_one<\/code>, which returns <code>true<\/code> if the absolute value of the difference of two adjacent elements is 0 or 1. The result of splitting this vector with the <code>differ_by_one<\/code> predicate is the following chunks of elements:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[1,1,2,3,2,2,1]\n[3,4]\n[8,7,6,7]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For a second example, let&#8217;s consider we have an input text that contains sequences of digits and letters. We want to produce a view of each chunk of characters of the same kind (either digits or letters):<\/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=\"\">bool same_kind(char const a, char const b)\n{\n    bool b1 = std::isdigit(a);\n    bool b2 = std::isdigit(b);\n\n    return (b1 &amp;&amp; b2) || (!b1 &amp;&amp; !b2);\n}\n\nstd::string s {\"1234abc56e789fghi\"};\nfor (auto const&amp; c : s | std::views::chunk_by(same_kind))\n{\n   print(c);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This time, the predicate is the <code>same_kind<\/code> function. This returns <code>true<\/code> if both arguments are digits or neither is a digit. The output of this snippet is the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[1,2,3,4]\n[a,b,c]\n[5,6]\n[e]\n[7,8,9]\n[f,g,h,i]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">See Also<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to learn more about these range adaptors see the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2021\/p2321r2.html\" target=\"_blank\" rel=\"noopener\" title=\"\">P2321R2: zip<\/a><\/li><li><a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2021\/p2442r1.html\" target=\"_blank\" rel=\"noopener\" title=\"\">P2442R1: Windowing range adaptors: views::chunk and views::slide<\/a><\/li><li><a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2021\/p2443r1.html\" target=\"_blank\" rel=\"noopener\" title=\"\">P2443R1: views::chunk_by<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Last year, I wrote a blog post about new C++23 range adaptors for joining and zipping. The C++23 standard includes a longer lists of range adapters (you can find a list here). Among them, there are several adaptors used for creating views consisting of chunks or &#8220;windows&#8221; of the elements of a given range. In &#8230; <a title=\"How to make chunks of a range in C++23\" class=\"read-more\" href=\"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/\" aria-label=\"Read more about How to make chunks of a range in C++23\">Read more<\/a><\/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,632,579],"class_list":["post-4599","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-c23","tag-ranges"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.3 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.\" \/>\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++23,ranges\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" \/>\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=\"How to make chunks of a range in C++23\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2023-01-16T19:25:24+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2023-01-16T19:25:27+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"How to make chunks of a range in C++23\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.\" \/>\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\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#article\",\"name\":\"How to make chunks of a range in C++23\",\"headline\":\"How to make chunks of a range in C++23\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/ranges_adjacent_view.png\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#articleImage\",\"width\":408,\"height\":359},\"datePublished\":\"2023-01-16T21:25:24+02:00\",\"dateModified\":\"2023-01-16T21:25:27+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#webpage\"},\"articleSection\":\"C++, C++, C++23, ranges\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#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\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#listItem\",\"name\":\"How to make chunks of a range in C++23\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#listItem\",\"position\":5,\"name\":\"How to make chunks of a range in C++23\",\"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\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#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\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/\",\"name\":\"How to make chunks of a range in C++23\",\"description\":\"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/01\\\/16\\\/how-to-make-chunks-of-a-range-in-cpp23\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2023-01-16T21:25:24+02:00\",\"dateModified\":\"2023-01-16T21:25:27+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":"How to make chunks of a range in C++23","description":"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/","robots":"max-image-preview:large","keywords":"c++,c++23,ranges","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#article","name":"How to make chunks of a range in C++23","headline":"How to make chunks of a range in C++23","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2023\/01\/ranges_adjacent_view.png","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#articleImage","width":408,"height":359},"datePublished":"2023-01-16T21:25:24+02:00","dateModified":"2023-01-16T21:25:27+02:00","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#webpage"},"articleSection":"C++, C++, C++23, ranges"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#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\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#listItem","name":"How to make chunks of a range in C++23"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#listItem","position":5,"name":"How to make chunks of a range in C++23","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\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#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\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/","name":"How to make chunks of a range in C++23","description":"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2023-01-16T21:25:24+02:00","dateModified":"2023-01-16T21:25:27+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":"How to make chunks of a range in C++23","og:description":"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.","og:url":"https:\/\/mariusbancila.ro\/blog\/2023\/01\/16\/how-to-make-chunks-of-a-range-in-cpp23\/","article:published_time":"2023-01-16T19:25:24+00:00","article:modified_time":"2023-01-16T19:25:27+00:00","twitter:card":"summary","twitter:title":"How to make chunks of a range in C++23","twitter:description":"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range."},"aioseo_meta_data":{"post_id":"4599","title":null,"description":"Learn how to use the C++23 range adaptors adjacent_view, slide_view, chunk_view and chunk_by_view to make chunks from a range.","keywords":[],"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":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"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":"2023-01-16 14:51:42","updated":"2026-10-18 15:43:46","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":1,"uagb_excerpt":"Last year, I wrote a blog post about new C++23 range adaptors for joining and zipping. The C++23 standard includes a longer lists of range adapters (you can find a list here). Among them, there are several adaptors used for creating views consisting of chunks or &#8220;windows&#8221; of the elements of a given range. In&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++23<\/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++23<\/span>","<span class=\"advgb-post-tax-term\">ranges<\/span>"]}},"comment_count":"1","relative_dates":{"created":"Posted 4 years ago","modified":"Updated 4 years ago"},"absolute_dates":{"created":"Posted on January 16, 2023","modified":"Updated on January 16, 2023"},"absolute_dates_time":{"created":"Posted on January 16, 2023 9:25 pm","modified":"Updated on January 16, 2023 9:25 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-1cb","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4599","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=4599"}],"version-history":[{"count":10,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4599\/revisions"}],"predecessor-version":[{"id":4615,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4599\/revisions\/4615"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=4599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=4599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=4599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}