fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // unary functor; performs '&&'
  9. template <typename T>
  10. struct AND
  11. {
  12. function<bool (const T&)> x;
  13. function<bool (const T&)> y;
  14.  
  15. AND(function<bool (const T&)> xx, function<bool (const T&)> yy)
  16. : x(xx), y(yy) {}
  17. bool operator() ( const T &arg ) { return x(arg) && y(arg); }
  18. };
  19.  
  20. // helper
  21. template <typename T>
  22. AND<T> And(function<bool (const T&)> xx, function<bool (const T&)> yy)
  23. {
  24. return AND<T>(xx,yy);
  25. }
  26.  
  27. bool is_odd(int n) { return n%2; }
  28. bool is_big(int n) { return n>5; }
  29.  
  30.  
  31. bool big_odd_exists( vector<int>::iterator first, vector<int>::iterator last )
  32. {
  33. /*
  34.   function<bool (const int &)> fun1 = is_odd;
  35.   function<bool (const int &)> fun2 = is_big;
  36.   */
  37.  
  38. //return any_of( first, last, And( fun1, fun2 ) ); // instantiating an And object
  39. return any_of( first, last, And<int>( is_odd, is_big ) );
  40. }
  41.  
  42. int main()
  43. {
  44. std::vector<int> n = {1, 3, 5, 7, 9, 10, 11};
  45.  
  46. cout << "exists : " << big_odd_exists( n.begin(), n.end() ) << endl;
  47. }
  48.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
exists : 1