完成count_if

This commit is contained in:
邹晓航
2014-10-31 14:11:07 +08:00
parent 2016eb67ea
commit d4eab62cac

View File

@@ -333,6 +333,18 @@ namespace TinySTL{
}
return n;
}
//********** [count_if] ******************************
//********* [Algorithm Complexity: O(N)] ****************
template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type
count_if(InputIterator first, InputIterator last, UnaryPredicate pred){
typename iterator_traits<InputIterator>::difference_type n = 0;
for (; first != last; ++first){
if (pred(*first))
++n;
}
return n;
}
}