c++でcount関数は何をするものですか?
C++のcount()関数は、コンテナ中に含まれる要素の数をカウントするために使用される関数です。通常、vector、list、arrayなどの標準ライブラリコンテナで使用されます。
count() 関数の構文は次のとおりです。
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val);
firstとlastは集計対象範囲を指定するイテレータ、valは集計対象要素の値であり、指定された要素値valの個数を返します。
以下に例の使い方を記す。
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 1, 2, 3, 1, 2, 1};
int count = std::count(numbers.begin(), numbers.end(), 1);
std::cout << "Number of occurrences of 1: " << count << std::endl;
return 0;
}
1の出現回数: 4