std::count, std::count_if
ヘッダ <algorithm> で定義
|
||
(1) | ||
template< class InputIt, class T > typename iterator_traits<InputIt>::difference_type |
(C++20未満) | |
template< class InputIt, class T > constexpr typename iterator_traits<InputIt>::difference_type |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class T > typename iterator_traits<ForwardIt>::difference_type |
(2) | (C++17以上) |
(3) | ||
template< class InputIt, class UnaryPredicate > typename iterator_traits<InputIt>::difference_type |
(C++20未満) | |
template< class InputIt, class UnaryPredicate > constexpr typename iterator_traits<InputIt>::difference_type |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > typename iterator_traits<ForwardIt>::difference_type |
(4) | (C++17以上) |
範囲 [first, last)
内の特定の基準を満たす要素の数を返します。
value
と等しい要素を数えます。p
が true を返す要素を数えます。policy
に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します目次 |
[編集] 引数
first, last | - | 調べる要素の範囲 |
value | - | 検索する値 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
p | - | 要求された要素に対して true を返す単項述語。 式 p(v) は |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
|
[編集] 戻り値
条件を満たす要素の数。
[編集] 計算量
ちょうど last
- first
回の比較または述語の適用。
[編集] 例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集] ノート
いかなる追加の基準もない範囲 [first, last)
の要素数については、 std::distance を参照してください。
[編集] 実装例
libstdc++ と libc++ の count の実装も参照してください。
libstdc++ と libc++ の count_if の実装も参照してください。
1つめのバージョン |
---|
template<class InputIt, class T> typename iterator_traits<InputIt>::difference_type count(InputIt first, InputIt last, const T& value) { typename iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } |
2つめのバージョン |
template<class InputIt, class UnaryPredicate> typename iterator_traits<InputIt>::difference_type count_if(InputIt first, InputIt last, UnaryPredicate p) { typename iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (p(*first)) { ret++; } } return ret; } |
[編集] 例
#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; // determine how many integers in a std::vector match a target value. int target1 = 3; int target2 = 5; int num_items1 = std::count(v.begin(), v.end(), target1); int num_items2 = std::count(v.begin(), v.end(), target2); std::cout << "number: " << target1 << " count: " << num_items1 << '\n'; std::cout << "number: " << target2 << " count: " << num_items2 << '\n'; // use a lambda expression to count elements divisible by 3. int num_items3 = std::count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;}); std::cout << "number divisible by three: " << num_items3 << '\n'; }
出力:
number: 3 count: 2 number: 5 count: 0 number divisible by three: 3
[編集] 関連項目
2つのイテレータ間の距離を返します (関数) | |
特定の基準を満たす要素の数を返します (ニーブロイド) |