std:: search_n
|
ヘッダーで定義
<algorithm>
|
||
| (1) | ||
|
template
<
class
ForwardIt,
class
Size,
class
T
>
ForwardIt search_n
(
ForwardIt first, ForwardIt last,
|
(C++20以降constexpr)
(C++26まで) |
|
|
template
<
class
ForwardIt,
class
Size,
class
T
=
typename
std::
iterator_traits
|
(C++26以降) | |
| (2) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
Size,
class
T
>
|
(C++17以降)
(C++26まで) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
Size,
|
(C++26以降) | |
| (3) | ||
|
template
<
class
ForwardIt,
class
Size,
class
T,
class
BinaryPred
>
ForwardIt search_n
(
ForwardIt first, ForwardIt last,
|
(constexpr C++20以降)
(C++26まで) |
|
|
template
<
class
ForwardIt,
class
Size,
class
T
=
typename
std::
iterator_traits
|
(C++26以降) | |
| (4) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
Size,
class
T,
class
BinaryPred
>
|
(C++17以降)
(C++26まで) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
Size,
class
T
=
typename
std::
iterator_traits
|
(C++26以降) | |
範囲
[
first
,
last
)
内で、指定された
value
と等しい
count
個の同一要素から成る最初の連続シーケンスを検索します。
|
std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> が true であること。 |
(C++20まで) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> が true であること。 |
(C++20以降) |
目次 |
パラメータ
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| count | - | 検索するシーケンスの長さ |
| value | - | 検索する要素の値 |
| policy | - | 使用する実行ポリシー |
| p | - |
要素が等しいと見なされるべき場合に
true
を返す二項述語。
述語関数のシグネチャは以下と同等であるべきです: bool pred ( const Type1 & a, const Type2 & b ) ;
シグネチャが
const
&
を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、
値カテゴリ
に関係なく型(おそらくconstの)
|
| 型要件 | ||
-
ForwardIt
は
LegacyForwardIterator
の要件を満たさなければなりません。
|
||
-
BinaryPred
は
BinaryPredicate
の要件を満たさなければなりません。
|
||
-
Size
は
変換可能
でなければなりません
整数型
へ。
|
||
戻り値
count
が正の場合、範囲
[
first
,
last
)
内で見つかった最初のシーケンスの先頭を指すイテレータを返します。
シーケンス内の各イテレータ
it
は次の条件を満たす必要があります:
そのようなシーケンスが見つからない場合、 last が返されます。
count がゼロまたは負の場合、 first が返されます。
計算量
与えられた N を std:: distance ( first, last ) として:
例外
ExecutionPolicy
という名前のテンプレートパラメータを持つオーバーロードは、
以下のようにエラーを報告します:
-
アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、
ExecutionPolicyが 標準ポリシー のいずれかであるとき、 std::terminate が呼び出されます。その他のExecutionPolicyについては、動作は実装定義です。 - アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされます。
実装例
| search_n (1) |
|---|
template<class ForwardIt, class Size, class T = typename std::iterator_traits<ForwardIt>::value_type> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value) { if (count <= 0) return first; for (; first != last; ++first) { if (!(*first == value)) continue; ForwardIt candidate = first; for (Size cur_count = 1; true; ++cur_count) { if (cur_count >= count) return candidate; // 成功 ++first; if (first == last) return last; // リストを走査し尽くした if (!(*first == value)) break; // 連続する要素が少なすぎる } } return last; } |
| search_n (3) |
template<class ForwardIt, class Size, class T = typename std::iterator_traits<ForwardIt>::value_type, class BinaryPred> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPred p) { if (count <= 0) return first; for (; first != last; ++first) { if (!p(*first, value)) continue; ForwardIt candidate = first; for (Size cur_count = 1; true; ++cur_count) { if (cur_count >= count) return candidate; // 成功 ++first; if (first == last) return last; // リストを走査し尽くした if (!p(*first, value)) break; // 連続する要素が少なすぎる } } return last; } |
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | リスト初期化 アルゴリズム用 ( 1-4 ) |
例
#include <algorithm> #include <cassert> #include <complex> #include <iostream> #include <iterator> #include <vector> template<class Container, class Size, class T> constexpr bool consecutive_values(const Container& c, Size count, const T& v) { return std::search_n(std::begin(c), std::end(c), count, v) != std::end(c); } int main() { constexpr char sequence[] = ".0_0.000.0_0."; static_assert(consecutive_values(sequence, 3, '0')); for (int n : {4, 3, 2}) std::cout << std::boolalpha << "Has " << n << " consecutive zeros: " << consecutive_values(sequence, n, '0') << '\n'; std::vector<std::complex<double>> nums{{4, 2}, {4, 2}, {1, 3}}; #ifdef __cpp_lib_algorithm_default_value_type auto it = std::search_n(nums.cbegin(), nums.cend(), 2, {4, 2}); #else auto it = std::search_n(nums.cbegin(), nums.cend(), 2, std::complex<double>{4, 2}); #endif assert(it == nums.begin()); }
出力:
Has 4 consecutive zeros: false Has 3 consecutive zeros: true Has 2 consecutive zeros: true
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 283 | C++98 |
T
は
EqualityComparable
が要求されていたが、
InputIt
の値型が常に
T
ではない
|
要求を削除 |
| LWG 426 | C++98 |
計算量の上限が
N·count
であったが、
count が負の場合に問題が生じる |
上限を
0
( count が非正の場合) |
| LWG 714 | C++98 |
count
>
0
の場合、計算量の上限が
N·count
であったが、
最悪ケースでの比較/操作回数は常に
N
|
上限を
N
に変更
|
| LWG 2150 | C++98 | 「シーケンスの出現」の条件が不正確 | 修正済み |
関連項目
|
特定の範囲内で要素の最後のシーケンスを見つける
(関数テンプレート) |
|
|
(C++11)
|
特定の条件を満たす最初の要素を見つける
(関数テンプレート) |
|
要素の範囲の最初の出現を検索する
(関数テンプレート) |
|
|
(C++20)
|
範囲内で要素の連続したコピーの最初の出現を検索する
(アルゴリズム関数オブジェクト) |