泛型算法系列9:count()&&count_if()
2009-08-10 13:07
429 查看
#include <iostream> #include <algorithm> #include <vector> #include <algorithm> #include <fstream> using namespace std; template<class _InIt, class _Ty> inline typename iterator_traits<_InIt>::difference_type _Count(_InIt _First, _InIt _Last, const _Ty& _Val) { // count elements that match _Val _DEBUG_RANGE(_First, _Last); typename iterator_traits<_InIt>::difference_type _Cnt = 0; for (; _First != _Last; ++_First) if (*_First == _Val) ++_Cnt; return (_Cnt); } /************************************************************************/ /* */ /************************************************************************/ int main(int argc, char* argv[]) { int a[]={1,1,2,3,4,5,4,4,5}; int elem_count = 0; elem_count=count( a,a+9,5 ); cout<< elem_count << " times/n"; return 0; }
下面是count_if的例子
#include <iostream> #include <algorithm> #include <vector> #include <algorithm> #include <fstream> #include <list> using namespace std; class Even { public: bool operator()( int val ) { return val%2 ? false : true; } }; int main() { int ia[] = {0,1,1,2,3,5,8,13,21,34}; list< int> ilist( ia, ia+10 ); /* * unsupported in current implementation ***************************************************** typedef iterator_traits<InputIterator>::distance_type distance_type; distance_type ia_count, list_count; // count even elements: 4 ia_count = count_if( &ia[0], &ia[10], Even() ); list_count = count_if( ilist.begin(), ilist_end(), bind2nd(less<int>(),10) ); ****************************************************** */ int ia_count =count_if( &ia[0], &ia[10], Even()); // generates: // count_if(): there are 4 elements that are even. cout << "count_if(): there are " << ia_count << " elements that are even./n"; int list_count =count_if( ilist.begin(), ilist.end(), bind2nd(less<int>(),10)); // generates: // count_if(): there are 7 elements that are less than 10. cout << "count_if(): there are " << list_count << " elements that are less than 10./n"; return 0; } /************************************************************************/ /* template<class _InIt, class _Pr> inline typename iterator_traits<_InIt>::difference_type _Count_if(_InIt _First, _InIt _Last, _Pr _Pred) { // count elements satisfying _Pred _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Pred); typename iterator_traits<_InIt>::difference_type _Count = 0; for (; _First != _Last; ++_First) if (_Pred(*_First)) ++_Count; return (_Count); } */ /************************************************************************/ /************************************************************************/ /* */ template<class _Fn2> class binder2nd : public unary_function<typename _Fn2::first_argument_type, typename _Fn2::result_type> { // functor adapter _Func(left, stored) public: typedef unary_function<typename _Fn2::first_argument_type, typename _Fn2::result_type> _Base; typedef typename _Base::argument_type argument_type; typedef typename _Base::result_type result_type; binder2nd(const _Fn2& _Func, const typename _Fn2::second_argument_type& _Right) : op(_Func), value(_Right) { // construct from functor and right operand } result_type operator()(const argument_type& _Left) const { // apply functor to operands return (op(_Left, value)); } result_type operator()(argument_type& _Left) const { // apply functor to operands return (op(_Left, value)); } protected: _Fn2 op; // the functor to apply typename _Fn2::second_argument_type value; // the right operand }; /************************************************************************/
相关文章推荐
- 泛型算法系列32:replace()&&replace_copy()&&replace_if()&&replace_copy_if()
- 泛型算法系列6:find()&&find_if()
- 泛型算法系列19:remove_if()&&remove_copy_if()
- 简单的程序诠释C++ STL算法系列: find & find_if
- 泛型算法系列29:unique()&&unique_copy()
- 泛型算法系列33:merge()&&inplace_merge()
- 泛型算法系列35:set_difference()&&set_intersection()&&set_symmetric_difference()&&set_union()
- 泛型算法系列5:lower_bound()&&upper_bound()
- 泛型算法系列17:generate_n()&&generate()
- 泛型算法系列28:partial_sort()&&partial_sort_copy()&&partial_sum()
- 泛型算法系列20:reverse()&&reverse_copy()
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 泛型算法系列30:stable_partition()&amp;&amp;partition()&&stable_sort()
- <leetcode系列> Count And Say
- 泛型算法系列14:random_shuffle()&&iter_swap()&&swap()
- 泛型算法系列16:max()&&min()&&min_element()&&max_element()
- 泛型算法系列18:remove()&&remove_copy()
- 泛型算法系列21:retate()&&retate_copy()
- 泛型算法系列22:search()&&search_n()
- 泛型算法系列27:next_permutation()&&prev_permutation()