fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. template<class InputIt, class UnaryPredicate>
  6. typename iterator_traits<InputIt>::difference_type
  7. Count_if(InputIt first, InputIt last, UnaryPredicate * p)
  8. {
  9. typename iterator_traits<InputIt>::difference_type ret = 0;
  10. for (; first != last; ++first) {
  11. if (p(*first)) {
  12. ret++;
  13. }
  14. }
  15. return ret;
  16. }
  17.  
  18. bool even(int i)
  19. {
  20. return i%2 == 0;
  21. }
  22.  
  23. int main(int argc, const char * argv[])
  24. {
  25. vector<int> v = { 1,2,3,4,5,6,7,8,9,0};
  26.  
  27. int total = 0;
  28. //cout << Count_if(v.begin(),v.end(),[](int x) { return x%2 == 0; }) << endl;
  29. //cout << Count_if(v.begin(),v.end(),[&total](int x) { ++total; return x%2 == 0; }) << endl;
  30. cout << Count_if(v.begin(),v.end(),even) << endl;
  31.  
  32. }
  33.  
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
5