プログラミング道場 ACM/ICPC

特定要素のカウント

最終更新:

kit

- view
だれでも歓迎! 編集
/*
	template<class _InIt, class _Ty>
	typename iterator_traits<_InIt>::difference_type
		count(_InIt _First, _InIt _Last, const _Ty& _Val)

	template<class _InIt, class _Pr>
	typename iterator_traits<_InIt>::difference_type
		count_if(_InIt _First, _InIt _Last, _Pr _Pred)
*/

#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;

bool f( int n )
{ return n >= 3; }

int main()
{
	int a[] = {1,1,2,2,3,3,4,4};

	// 2 {3,3} operator ==により比較される
	cout << count(a,a+8,3) << endl;

	// 4 {3,3,4,4}
	cout << count_if(a,a+8,f) << endl;

	return 0;
}
人気記事ランキング
ウィキ募集バナー