std::count, std::count_if
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
template< class InputIt, class T > typename iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ); |
(C++20未満) | |
template< class InputIt, class T > constexpr typename iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class T > typename iterator_traits<ForwardIt>::difference_type count( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T &value ); |
(2) | (C++17以上) |
| (3) | ||
template< class InputIt, class UnaryPredicate > typename iterator_traits<InputIt>::difference_type count_if( InputIt first, InputIt last, UnaryPredicate p ); |
(C++20未満) | |
template< class InputIt, class UnaryPredicate > constexpr typename iterator_traits<InputIt>::difference_type count_if( InputIt first, InputIt last, UnaryPredicate p ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > typename iterator_traits<ForwardIt>::difference_type count_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(4) | (C++17以上) |
範囲 [first, last) 内の特定の基準を満たす要素の数を返します。
1)
value と等しい要素を数えます。3) 述語
p が true を返す要素を数えます。2,4) (1,3) と同じですが、
policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します引数
| first, last | - | 調べる要素の範囲 |
| value | - | 検索する値 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| p | - | 要求された要素に対して true を返す単項述語。 式 |
| 型の要件 | ||
-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;
}
|
例
Run this code
#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つのイテレータ間の距離を返します (関数) | |
| 特定の基準を満たす要素の数を返します (ニーブロイド) |