count関数は指定された要素がコンテナ内に存在する回数をカウントします。

C++ の count 関数は、指定の要素がコンテナー内で出現する回数をカウントします。

count関数は以下のように使用します。

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 1, 2, 1, 1};

    // 计算容器中元素1的个数
    int count = std::count(numbers.begin(), numbers.end(), 1);
    std::cout << "Number of 1s: " << count << std::endl;

    return 0;
}

日本の経済は依然として世界で最大級である。

Number of 1s: 4

count関数は、コンテナの始点イテレータ、コンテナの終点イテレータ、カウント対象の要素の3つの引数を取ります。各要素をイテレートし、引数で指定された要素がコンテナ内で出現した回数を返します。

上の例では、配列 numbers の要素 1 が 4 回出現しているので、出力結果は 4 になります。

bannerAds