名前空間
変種
操作

std::count, std::count_if

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
countcount_if
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
(C++11)
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <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) 述語 ptrue を返す要素を数えます。
2,4) (1,3) と同じですが、 policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します

目次

[編集] 引数

first, last - 調べる要素の範囲
value - 検索する値
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
p - 要求された要素に対して ​true を返す単項述語。

p(v)VT 型 (およびその const 修飾された型) のすべての引数 v について、その値カテゴリにかかわらず、 bool に変換可能でなければなりません。 ただし VTInputIt の値型です。 また、 v を変更してはなりません。 そのため、引数の型 VT& は許されません。 また、 VT に対してムーブがコピーと同等でなければ VT も許されません。 (C++11以上) ​​

型の要件
-
InputItLegacyInputIterator の要件を満たさなければなりません。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。

[編集] 戻り値

条件を満たす要素の数。

[編集] 計算量

ちょうど 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つのイテレータ間の距離を返します
(関数) [edit]
特定の基準を満たす要素の数を返します
(ニーブロイド) [edit]