fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4.  
  5. template <typename Container, typename ValueGetter, typename SizeType, typename T1, typename T2>
  6. void Count(Container& c, ValueGetter valueGetter, T1&& t1, SizeType& out1, T2&& t2, SizeType& out2)
  7. {
  8. using GetVal = decltype(valueGetter(c[0]));
  9. static_assert(std::is_same<std::decay_t<T1>, GetVal>::value, "Types don't match!");
  10. static_assert(std::is_same<std::decay_t<T2>, GetVal>::value, "Types don't match!");
  11.  
  12. for (auto& elm : c)
  13. {
  14. const auto& val = valueGetter(elm);
  15. if (val == t1) ++out1;
  16. else if (val == t2) ++out2;
  17. }
  18. }
  19.  
  20. struct Foo
  21. {
  22. int GetI() { return i; }
  23. int i = 0;
  24. };
  25.  
  26. int main()
  27. {
  28. std::vector<Foo> f(10);
  29. f[0].i = f[3].i = f[7].i = 37;
  30. f[1].i = 1;
  31.  
  32. unsigned i0 = 0;
  33. unsigned i37 = 0;
  34.  
  35. Count(f, [](auto& f) { return f.GetI(); },
  36. 0, i0,
  37. 37, i37);
  38.  
  39. std::cout << i0 << " " << i37;
  40. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
6 3