fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // 3. Implement count() yourself. Test it.
  7.  
  8. template<typename Iter, typename T>
  9. typename iterator_traits<Iter>::value_type
  10. count(Iter first, Iter last, T value)
  11. // T should be of type int or double
  12. {
  13. typename iterator_traits<Iter>::value_type sum = 0;
  14. for (Iter it = first; it != last; it++)
  15. {
  16. if (*it == value)
  17. {
  18. sum++;
  19. }
  20. }
  21. return sum;
  22. }
  23.  
  24. int main()
  25. {
  26. vector<int> v {1, 2, 3, 1, 5};
  27. cout << count(v.begin(), v.end(), 1) << endl;
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
2