最終更新日時(UTC):
が更新

履歴 編集

function template
<algorithm>

std::ranges::contains(C++23)

namespace ranges {
  template <input_iterator I,
            sentinel_for<I> S,
            class T,
            class Proj = identity>
    requires indirect_binary_predicate<
               ranges::equal_to,
               projected<I, Proj>,
               const T*
             >
  constexpr bool
    contains(I first,
             S last,
             const T& value,
             Proj proj = {}); // (1) C++23
  template <input_iterator I,
            sentinel_for<I> S,
            class Proj = identity,
            class T = projected_value_t<I, Proj>>
    requires indirect_binary_predicate<
               ranges::equal_to,
               projected<I, Proj>,
               const T*
             >
  constexpr bool
    contains(I first,
             S last,
             const T& value,
             Proj proj = {}); // (1) C++26

  template <input_range R,
            class T,
            class Proj = identity>
    requires indirect_binary_predicate<
               ranges::equal_to,
               projected<iterator_t<R>, Proj>,
               const T*
             >
  constexpr bool
    contains(R&& r,
             const T& value,
             Proj proj = {}); // (2) C++23
  template <input_range R,
            class Proj = identity,
            class T = projected_value_t<iterator_t<R>, Proj>>
    requires indirect_binary_predicate<
               ranges::equal_to,
               projected<iterator_t<R>, Proj>,
               const T*
             >
  constexpr bool
    contains(R&& r,
             const T& value,
             Proj proj = {}); // (2) C++26
}

概要

指定された値が含まれるか調べる。

戻り値

ranges::find(std::move(first), last, value, proj) != last

計算量

最大で last - first 回比較を行う

備考

  • (1), (2) :
    • C++26 : 引数として波カッコ初期化{}を受け付ける
      std::vector<T> v;
      bool found = std::ranges::contains(r, {a, b});
      

基本的な使い方

#include <algorithm>
#include <print>
#include <array>

int main() {
  constexpr std::array ar = { 3, 1, 4 };
  if (std::ranges::contains(ar, 1)) {
    std::println("found");
  } else {
    std::println("not found");
  }
}

出力

found

波カッコ初期化を入力として使用する (C++26)

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

struct Point {
  int x;
  int y;

  bool operator==(const Point& other) const = default;
};

int main() {
  std::vector<Point> v = {
    {1, 2},
    {3, 4},
    {5, 6}
  };

  bool found = std::ranges::contains(v, {3, 4});
  if (found) {
    std::println("found");
  } else {
    std::println("not found");
  }
}

出力

found

実装例

struct contains_impl {
  template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity>
    requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
  constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const {
    return ranges::find(std::move(first), last, value, proj) != last;
  }

  template<input_range R, class T, class Proj = identity>
    requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
  constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const {
    return (*this)(begin(r), end(r), value, ref(proj));
  }
};

inline constexpr contains_impl contains;

バージョン

言語

  • C++23

処理系

参照