algo/containssub.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{6, 8, 44, 9, 8, 2, 7};

  // check whether the range contains a sub-sequence of three elements that are greater than 10:
  std::println("{} has subset of three >10: {}",
               coll,  
               std::ranges::contains_subrange(coll, std::views::repeat(10, 3),
                                              std::ranges::greater{}));

  // check whether the range contains a sub-sequence of three elements that are greater than 7:
  std::println("{} has subset of three >7:  {}",
               coll,  
               std::ranges::contains_subrange(coll, std::views::repeat(7, 3),
                                              std::ranges::greater{}));
}