algo/shift.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <print>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<std::string> coll{"one", "two", "three", "four", "five",
                                "six", "seven", "eight", "nine"};
  std::println("coll: {}\n", coll);

  // move all elements 2 positions towards the back:
  auto ret1 = std::ranges::shift_right(coll, 2);
  std::println("coll: {}", coll);
  std::println(" ret: {}\n", ret1);

  // move all elements 4 positions towards the front:
  auto ret2 = std::ranges::shift_left(coll, 4);
  std::println("coll: {}", coll);
  std::println(" ret: {}", ret2);
}