std::ranges::contains, std::ranges::contains_subrange
ja.cppreference.net より
| ヘッダ <algorithm>
|
||
| で定義 |
||
template< std::input_iterator I, std::sentinel_for<I> S,
class T,
class Proj = std::identity >
requires std::indirect_binary_predicate
<ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr bool contains( I first, S last, const T& value, Proj proj = {} );
|
呼び出しシグネチャ | (1) (C++23 以降) |
template< std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
class T = std::projected_value_t<I, Proj> >
requires std::indirect_binary_predicate
<ranges::equal_to, std::projected<I, Proj>, const T*>
constexpr bool contains( I first, S last, const T& value, Proj proj = {} );
|
(C++26 まで) | |
template< ranges::input_range R,
class T,
class Proj = std::identity >
requires std::indirect_binary_predicate
<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr bool contains( R&& r, const T& value, Proj proj = {} );
|
(C++26 以降) | (2) (C++23 以降) |
template< ranges::input_range R,
class Proj = std::identity,
class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
requires std::indirect_binary_predicate
<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr bool contains( R&& r, const T& value, Proj proj = {} );
|
(C++26 まで) | |
template< std::forward_iterator I1, std::sentinel_for<I1> S1,
std::forward_iterator I2, std::sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool contains_subrange( I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(C++26 以降) | (3) |
template< ranges::forward_range R1, ranges::forward_range R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable
<ranges::iterator_t<R1>, ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
constexpr bool contains_subrange( R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(C++23 以降) | (4) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I, std::sized_sentinel_for<I> S,
class Proj = std::identity,
class T = std::projected_value_t<I, Proj> >
requires std::indirect_binary_predicate
<ranges::equal_to, std::projected<I, Proj>, const T*>
bool contains( Ep&& policy, I first, S last, const T& value, Proj proj = {} );
|
(C++23 以降) | (5) |
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R,
class Proj = std::identity,
class T = std::projected_value_t<ranges::iterator_t<R>, Proj> >
requires std::indirect_binary_predicate
<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T*>
bool contains( Ep&& policy, R&& r, const T& value, Proj proj = {} );
|
(C++26 以降) | (6) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I1, std::sized_sentinel_for<I1> S1,
std::random_access_iterator I2, std::sized_sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
bool contains_subrange( Ep&& policy, I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(C++26 以降) | (7) |
template< /*execution-policy*/ Ep,
/*sized-random-access-range*/ R1, /*sized-random-access-range*/ R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable
<ranges::iterator_t<R1>, ranges::iterator_t<R2>,
Pred, Proj1, Proj2>
bool contains_subrange( Ep&& policy, R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(C++26 以降) | (8) |
(C++26 以降)/*execution-policy*/このページ を参照してください。 /*sized-random-access-range*/ の定義については、このページ を参照してください。
1,2) ソース範囲がターゲット値を含むかどうかを確認します
value. 1) ソース範囲は
[first, last) です。 2) ソース範囲は
r です。 3,4) ターゲット範囲がソース範囲の部分範囲であるかどうかを確認します。
3) ソース範囲は
[first1, last1)、ターゲット範囲は [first2, last2) です。 4) ソース範囲は
r1、ターゲット範囲は r2 です。 5-8) (1-4) と同じですが、実行ポリシー
policy に従って実行されます。 このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト (非公式には niebloid として知られています) であり、すなわち:
- それらのいずれかを呼び出すときに、明示的なテンプレート引数リストを指定することはできません。
- それらは 引数依存の名前検索 (ADL) には表示されません。
- それらのいずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前検索 によって見つかった場合、引数依存の名前検索 は抑制されます。
パラメータ
| first/first1, last/last1 | - | ソースを定義するイテレータ-センチネルペア 範囲 |
| first2, last2 | - | ターゲットを定義するイテレータ-センチネルペア 範囲 |
| r/r1 | - | ソース範囲 |
| value | - | ターゲット値 |
| r2 | - | ターゲット範囲 |
| pred/pred1 | - | ソース範囲内の(射影された)要素に適用される述語 |
| pred2 | - | ターゲット範囲内の(射影された)要素に適用される述語 |
| proj/proj1 | - | ソース範囲内の要素に適用される射影 |
| proj2 | - | ターゲット範囲内の要素に適用される射影 |
| policy | - | execution policy を使用する |
戻り値
1)
ranges::find(std::move(first), last, value, proj) != last2)
ranges::find(r, value, proj) != ranges::end(r)3)
first2 == last2 || !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()4)
ranges::empty(r2) || !ranges::search(r1, r2, pred, proj1, proj2).empty()計算量
与えられた
- N を
ranges::distance(first, last)またはranges::distance(r)として、 - N1 を
ranges::distance(first1, last1)またはranges::distance(r1)として、 - N2 を
ranges::distance(first2, last2)またはranges::distance(r2)とする:
1,2) 最大で N 回の比較および
proj の適用。3,4) 最大で N1·N2 回の
pred と proj の適用。5,6) 𝓞(N) 回の比較および
proj の適用。7,8) 𝓞(N1·N2) 回の
pred と proj の適用。例外
5-8) 実行処理中:
- 並列化に必要な一時メモリリソースが利用できない場合、std::bad_alloc がスローされます。
- アルゴリズム引数を介してオブジェクトにアクセスしているときにキャッチされない例外がスローされた場合、動作は実行ポリシーによって決定されます(標準ポリシーの場合、std::terminate が呼び出されます)。
ノート
C++20では、containsをranges::find(haystack, needle) != ranges::end(haystack)で実装するか、contains_subrangeを!ranges::search(haystack, needle).empty()で実装できます。
ranges::contains_subrangeは、ranges::searchと同様に、std::searchとは異なり、検索器(たとえばstd::boyer_moore_searcherなど)をサポートしていません。
| 機能テストマクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_ranges_contains |
202207L |
(C++23) | ranges::containsおよびranges::contains_subrange
|
__cpp_lib_algorithm_default_value_type |
202403L |
(C++26) | リスト初期化 for algorithms (1,2) |
実装例
| contains (1,2) |
|---|
struct contains_fn
{
template<std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
class T = std::projected_value_t<I, Proj>>
requires std::indirect_binary_predicate
<ranges::equal_to, std::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<ranges::input_range R,
class Proj = std::identity,
class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
requires std::indirect_binary_predicate
<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const
{
return ranges::find(r, value, proj) != ranges::end(r);
}
};
inline constexpr contains_fn contains{};
|
| contains_subrange (3,4) |
struct contains_subrange_fn
{
template<std::forward_iterator I1, std::sentinel_for<I1> S1,
std::forward_iterator I2, std::sentinel_for<I2> S2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity>
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
return (first2 == last2) ||
!ranges::search(first1, last1, first2, last2,
pred, proj1, proj2).empty();
}
template<ranges::forward_range R1, ranges::forward_range R2,
class Pred = ranges::equal_to,
class Proj1 = std::identity, class Proj2 = std::identity>
requires std::indirectly_comparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
return ranges::empty(r2) ||
!ranges::search(r1, r2, pred, proj1, proj2).empty();
}
};
inline constexpr contains_subrange_fn contains_subrange{};
|
例
このコードを実行
#include <algorithm> #include <array> #include <complex> namespace ranges = std::ranges; int main() { constexpr auto haystack = std::array{3, 1, 4, 1, 5}; constexpr auto needle = std::array{1, 4, 1}; constexpr auto bodkin = std::array{2, 5, 2}; static_assert ( ranges::contains(haystack, 4) && !ranges::contains(haystack, 6) && ranges::contains_subrange(haystack, needle) && !ranges::contains_subrange(haystack, bodkin) ); constexpr std::array<std::complex<double>, 3> nums{{{1, 2}, {3, 4}, {5, 6}}}; #ifdef __cpp_lib_algorithm_default_value_type static_assert(ranges::contains(nums, {3, 4})); #else static_assert(ranges::contains(nums, std::complex<double>{3, 4})); #endif }
関連項目
(C++20)(C++20)(C++20) |
特定の条件を満たす最初の要素を検索する (アルゴリズム関数オブジェクト) |
(C++20) |
要素の範囲の最初の出現を検索する (アルゴリズム関数オブジェクト) |
(C++20) |
バイナリ検索を使用して範囲内に要素が存在するかどうかを判断する (アルゴリズム関数オブジェクト) |
(C++20) |
一方のシーケンスが他方の部分列であるかどうかを判断する (アルゴリズム関数オブジェクト) |
(C++20)(C++20)(C++20) |
範囲内のすべての要素、任意の要素、または要素が一つもない場合に述語がtrueであるかチェックする(アルゴリズム関数オブジェクト) |