std::search_n
提供: cppreference.com
ヘッダ <algorithm> で定義 | ||
(1) | ||
template<class ForwardIt, class Size, class T > ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value ); | (C++20以前) | |
template<class ForwardIt, class Size, class T > constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value ); | (C++20およびそれ以降) | |
template<class ExecutionPolicy, class ForwardIt, class Size, class T > ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Size count, const T& value ); | (2) | (C++17およびそれ以降) |
(3) | ||
template<class ForwardIt, class Size, class T, class BinaryPredicate > ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value, | (C++20以前) | |
template<class ForwardIt, class Size, class T, class BinaryPredicate > constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value, | (C++20およびそれ以降) | |
template<class ExecutionPolicy, class ForwardIt, class Size, class T, class BinaryPredicate > ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Size count, const T& value, | (4) | (C++17およびそれ以降) |
指定された値と等しい要素 count 個のシーケンス現れる最初の位置を範囲 [first, last)
から検索します。
1) 要素は
operator==
を用いて比較されます。3) 要素は指定された二項述語
p
を用いて比較されます。2,4)(1,3) と同じですが、
policy
に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。目次 |
[編集]引数
first, last | - | 調べる要素の範囲 |
count | - | 検索するシーケンスの長さ |
value | - | 検索する要素の値 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
p | - | 要素が等しいと扱われるべき場合に true を返す二項述語。 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type1 &a, const Type2 &b); シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。 |
型の要件 | ||
-ForwardIt は ForwardIterator の要件を満たさなければなりません。 |
[編集]戻り値
範囲 [first, last)
内の見つかったシーケンスの先頭を指すイテレータ。 そのようなシーケンスが見つからない場合は last
が返されます。
[編集]計算量
多くとも last - first
回の述語の適用。
[編集]例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集]実装例
1つめのバージョン |
---|
template<class ForwardIt, class Size, class T> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value){for(; first != last;++first){if(!(*first == value)){continue;} ForwardIt candidate = first; Size cur_count =0; while(true){++cur_count;if(cur_count == count){// successreturn candidate;}++first;if(first == last){// exhausted the listreturn last;}if(!(*first == value)){// too few in a rowbreak;}}}return last;} |
2つめのバージョン |
template<class ForwardIt, class Size, class T, class BinaryPredicate> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p){for(; first != last;++first){if(!p(*first, value)){continue;} ForwardIt candidate = first; Size cur_count =0; while(true){++cur_count;if(cur_count == count){// successreturn candidate;}++first;if(first == last){// exhausted the listreturn last;}if(!p(*first, value)){// too few in a rowbreak;}}}return last;} |
[編集]例
Run this code
#include <iostream>#include <algorithm>#include <iterator> template<class Container, class Size, class T>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(){constchar sequence[]="1001010100010101001010101"; std::cout<<std::boolalpha;std::cout<<"Has 4 consecutive zeros: "<< consecutive_values(sequence,4,'0')<<'\n';std::cout<<"Has 3 consecutive zeros: "<< consecutive_values(sequence,3,'0')<<'\n';}
出力:
Has 4 consecutive zeros: false Has 3 consecutive zeros: true
[編集]関連項目
指定された要素の並びが現れる最後の位置を探します (関数テンプレート) | |
(C++11) | 一定の基準を満たす最初の要素を探します (関数テンプレート) |
指定範囲の要素に対して検索を行います (関数テンプレート) |