function.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <vector>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <boost/phoenix/core.hpp>
  10. #include <boost/phoenix/function.hpp>
  11. using boost::phoenix::function;
  12. struct is_odd_
  13. {
  14. typedef bool result_type;
  15. template <typename Arg>
  16. bool operator()(Arg arg1) const
  17. {
  18. return arg1 % 2 == 1;
  19. }
  20. };
  21. function<is_odd_> is_odd;
  22. int
  23. main()
  24. {
  25. using boost::phoenix::arg_names::arg1;
  26. int init[] = { 2, 10, 4, 5, 1, 6, 8, 3, 9, 7 };
  27. std::vector<int> c(init, init + 10);
  28. typedef std::vector<int>::iterator iterator;
  29. // Find the first odd number in container c
  30. iterator it = std::find_if(c.begin(), c.end(), is_odd(arg1));
  31. if (it != c.end())
  32. std::cout << *it << std::endl; // if found, print the result
  33. return 0;
  34. }