- algorithm[meta header]
- std::ranges[meta namespace]
- function template[meta id-type]
- cpp23[meta cpp]
namespace std::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
template <execution-policy Ep,
random_access_iterator I,
sized_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*
>
bool
contains(Ep&& exec,
I first,
S last,
const T& value,
Proj proj = {}); // (3) C++26
template <execution-policy Ep,
sized-random-access-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*
>
bool
contains(Ep&& exec,
R&& r,
const T& value,
Proj proj = {}); // (4) C++26
}- execution-policy[link /reference/execution/execution-policy.md]
- random_access_iterator[link /reference/iterator/random_access_iterator.md]
- sized_sentinel_for[link /reference/iterator/sized_sentinel_for.md]
- sized-random-access-range[link /reference/ranges/sized-random-access-range.md]
指定された値が含まれるか調べる。
- (1): イテレータ範囲を指定する
- (2): Rangeを直接指定する
- (3): (1)の並列アルゴリズム版。実行ポリシーを指定する
- (4): (2)の並列アルゴリズム版。実行ポリシーを指定する
ranges::find(std::move(first), last, value, proj) != last- ranges::find[link ranges_find.md]
最大で last - first 回比較を行う
- (1), (2) :
- C++26 : 引数として波カッコ初期化
{}を受け付けるstd::vector<T> v; bool found = std::ranges::contains(r, {a, b});
- C++26 : 引数として波カッコ初期化
#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");
}
}- std::ranges::contains[color ff0000]
found
#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");
}
}- std::ranges::contains[color ff0000]
found
#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5};
std::cout << std::boolalpha;
// 並列に値が含まれるかを判定
bool result = std::ranges::contains(std::execution::par, v, 4);
std::cout << result << std::endl;
}- std::ranges::contains[color ff0000]
true
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;- ranges::find[link ranges_find.md]
- C++23
- Clang: ??
- GCC: ??
- ICC: ??
- Visual C++: ??
- N4950 27 Algorithms library
- P2248R8 Enabling list-initialization for algorithms
- C++26で波カッコ初期化 (リスト初期化) に対応した
- P3179R9 C++ parallel range algorithms