{"id":1763,"date":"2021-03-04T07:00:00","date_gmt":"2021-03-04T12:00:00","guid":{"rendered":"http:\/\/www.jaimerios.com\/?p=1763"},"modified":"2025-12-02T23:36:12","modified_gmt":"2025-12-02T23:36:12","slug":"c-stdrotate","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=1763","title":{"rendered":"C++ std::rotate"},"content":{"rendered":"\n<p>This is an interesting template function, defined in , which moves elements of a container class, much in the same way when you rotate the number wheel on a luggage combination lock: rotate the wheel to the left, all of the numbers move along with that rotation; rotate the wheel to the right<br> and the numbers do the same thing, but in the opposite motion.<\/p>\n\n\n\n<p>Much like the arrow on the combination lock, the rotate&#8217;s second parameter, <code>ForwardIt n_first<\/code>, is the element that ends up being under the arrow.<br>\nBy being under the arrow, we mean that <code>ForwardIt n_first<\/code> ends up at the front of the container, and the item immediately before it ends up at the end of the container.<\/p>\n\n\n\n<p>Here&#8217;s an example of moving the metaphorical wheel, left then right:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;algorithm>\n#include &lt;iostream>\n#include &lt;vector>\n\nvoid print_elements(std::vector&lt;int> values);\n\n\/************************************************************************\/\nint main(int, char**)\n{\n  auto combination_lock = std::vector&lt;int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};\n  print_elements(combination_lock);\n  std::cout &lt;&lt; \" ################ \\n\";\n\n  \/\/ Rotate our wheel to the left 5 positions\n  std::rotate(combination_lock.begin(), combination_lock.begin() + 5, combination_lock.end());\n  print_elements(combination_lock);\n  std::cout &lt;&lt; \" ################ \\n\";\n\n  \/\/ Rotate our wheel to the right 3 positions\n  std::rotate(combination_lock.rbegin(), combination_lock.rbegin() + 3, combination_lock.rend());\n  print_elements(combination_lock);\n}\n\n\/************************************************************************\/\nvoid print_elements(std::vector&lt;int> values)\n{\n  for (auto&amp; val : values)\n  {\n    std::cout &lt;&lt; val &lt;&lt; \"\\n\";\n  }\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">#include<\/span><span style=\"color: #569CD6\"> <\/span><span style=\"color: #CE9178\">&lt;algorithm&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">#include<\/span><span style=\"color: #569CD6\"> <\/span><span style=\"color: #CE9178\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">#include<\/span><span style=\"color: #569CD6\"> <\/span><span style=\"color: #CE9178\">&lt;vector&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">void<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">print_elements<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">vector<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">&gt; <\/span><span style=\"color: #9CDCFE\">values<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/************************************************************************\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">main<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">char**<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> combination_lock = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::vector&lt;<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">&gt;{<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #DCDCAA\">print_elements<\/span><span style=\"color: #D4D4D4\">(combination_lock);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::cout &lt;&lt; <\/span><span style=\"color: #CE9178\">&quot; ################ <\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">  \/\/ Rotate our wheel to the left 5 positions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">rotate<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">combination_lock<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">begin<\/span><span style=\"color: #D4D4D4\">(), <\/span><span style=\"color: #9CDCFE\">combination_lock<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">begin<\/span><span style=\"color: #D4D4D4\">() + <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">combination_lock<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">end<\/span><span style=\"color: #D4D4D4\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #DCDCAA\">print_elements<\/span><span style=\"color: #D4D4D4\">(combination_lock);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::cout &lt;&lt; <\/span><span style=\"color: #CE9178\">&quot; ################ <\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">  \/\/ Rotate our wheel to the right 3 positions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">rotate<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">combination_lock<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">rbegin<\/span><span style=\"color: #D4D4D4\">(), <\/span><span style=\"color: #9CDCFE\">combination_lock<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">rbegin<\/span><span style=\"color: #D4D4D4\">() + <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">combination_lock<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">rend<\/span><span style=\"color: #D4D4D4\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #DCDCAA\">print_elements<\/span><span style=\"color: #D4D4D4\">(combination_lock);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/************************************************************************\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">void<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">print_elements<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">vector<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">&gt; <\/span><span style=\"color: #9CDCFE\">values<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\">&amp; val : values)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::cout &lt;&lt; val &lt;&lt; <\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is an interesting template function, defined in , which moves elements of a container class, much in the same way when you rotate the number wheel on a luggage combination lock: rotate the wheel to the left, all of the numbers move along with that rotation; rotate the wheel to the right and the &#8230; <a title=\"C++ std::rotate\" class=\"read-more\" href=\"https:\/\/jaimerios.com\/?p=1763\" aria-label=\"Read more about C++ std::rotate\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[143],"class_list":["post-1763","post","type-post","status-publish","format-standard","hentry","category-coding","tag-cpp"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1763"}],"version-history":[{"count":1,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions\/1892"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}