algo/startsbelow.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 <ranges>
#include <algorithm>
#include <functional>

int main()
{
  std::vector<int> coll{8, 15, 7, 11, 8, 0};

  // check whether the first three elements are less than 10:
  auto sub1 = std::views::repeat(10, 3);  // 10, 10, 10
  std::println("{} starts below {}: {}",
               coll, sub1,
               std::ranges::starts_with(coll, sub1,
                                        std::ranges::less{}));

  // check whether the first three elements are less than 20:
  auto sub2 = std::views::repeat(20, 3);  // 20, 20, 20
  std::println("{} starts below {}: {}",
               coll, sub2,
               std::ranges::ends_with(coll, sub2,
                                      std::ranges::less{}));
}