The Wayback Machine - https://web.archive.org/web/20170908035606/http://ja.cppreference.com:80/w/cpp/algorithm/count
名前空間
変種
操作

std::count, std::count_if

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を非改変
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
all_of
any_of
none_of
(C++11)
(C++11)
(C++11)
for_each
count
count_if
mismatch
equal
シーケンス動作を変更する
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
操作を仕切る
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)ソート操作
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
バイナリ検索操作(ソート範囲で)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)操作を設定します
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ヒープ操作
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最小値/最大値操作
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
数値演算
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cライブラリ
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template<class InputIt, class T >

typename iterator_traits<InputIt>::difference_type

    count( InputIt first, InputIt last, const T &value );
(1)
template<class InputIt, class UnaryPredicate >

typename iterator_traits<InputIt>::difference_type

    count_if( InputIt first, InputIt last, UnaryPredicate p );
(2)

[first, last)範囲内の、特定の条件を満たす要素数を返す.最初のバージョンはvalueに等しい要素をカウントし、2番目のバージョンは述語関数pの戻り値がtrueである要素をカウントする.

目次

[編集]パラメータ

first, last -
検討する要素の範囲
Original:
the range of elements to examine
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
value -
検索する値
Original:
the value to search for
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
p - unary predicate which returns ​true
必要な要素のために
Original:
for the required elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const&, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

型の要件
-
InputItInputIterator

の要求を満足しなければなりません。

[編集]値を返します

条件を満たす要素の数.
Original:
number of elements satisfying the condition.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]複雑性

まさにlast - first比較/述語のアプリケーション
Original:
exactly last - first comparisons / applications of the predicate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]可能な実装

First version
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;}
Second version
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;}

[編集]

次のコードは、どのように多くの整数countでは目標値に一致するかを判断するためにstd::vectorを使用しています.
Original:
The following code uses count to determine how many integers in a std::vector match a target value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>#include <iostream>#include <vector>   int main(){int data[]={1, 2, 3, 4, 4, 3, 7, 8, 9, 10};std::vector<int> v(data, data+10);   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';}

出力:

number: 3 count: 2 number: 5 count: 0

この例では、3で割り切れる要素をカウントするラムダ式を使用しています.
Original:
This example uses a ラムダ式 to count elements divisible by 3.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>#include <iostream>#include <vector>   int main(){int data[]={1, 2, 3, 4, 4, 3, 7, 8, 9, 10};std::vector<int> v(data, data+10);   int num_items1 = std::count_if(v.begin(), v.end(), [](int i){return i %3==0;});   std::cout<<"number divisible by three: "<< num_items1 <<'\n';}

出力:

number divisible by three: 3
close