Advertisement
Radfler

::contains

Jan 4th, 2021
1,762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iterator>
  4. #include <ranges>
  5.  
  6. namespace impl {
  7.     struct contains_fn {
  8.         template<std::input_iterator I, std::sentinel_for<I> S,
  9.                  typename T, typename Proj = std::identity>
  10.             requires std::indirect_binary_predicate<
  11.                 std::ranges::equal_to,
  12.                 std::projected<I, Proj>,
  13.                 const T*
  14.             >
  15.         constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const {
  16.             return std::ranges::find(first, last, value, std::ref(proj)) != last;
  17.         }
  18.  
  19.         template<std::ranges::input_range R, typename T, typename Proj = std::identity>
  20.             requires std::indirect_binary_predicate<
  21.                 std::ranges::equal_to,
  22.                 std::projected<std::ranges::iterator_t<R>, Proj>,
  23.                 const T*
  24.             >
  25.         constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const {
  26.             return (*this)(std::ranges::begin(r), std::ranges::end(r), value, std::ref(proj));
  27.         }
  28.     };
  29. }
  30.  
  31. inline constexpr impl::contains_fn contains{};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement