fork(3) download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. bool IsEven(int n) {
  8. return (n % 2) == 0;
  9. }
  10.  
  11. int main() {
  12. // Test vector
  13. vector<int> vec{ 11, 22, 33, 44, 55 };
  14.  
  15. // Using functional approach
  16. auto n = count_if(vec.begin(), vec.end(),
  17. bind(logical_not<bool>(),
  18. bind(modulus<int>(), placeholders::_1, 2)));
  19. cout << n << endl;
  20.  
  21. // Using lambdas
  22. n = count_if(vec.begin(), vec.end(),
  23. [](int n) { return (n % 2) == 0; });
  24. cout << n << endl;
  25.  
  26. // Using boolean returning ad hoc function
  27. n = count_if(vec.begin(), vec.end(), IsEven);
  28. cout << n << endl;
  29. }
  30.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
2
2
2