transform_seq.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/local_function
  6. #include <boost/local_function.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <algorithm>
  9. #include <vector>
  10. int main(void) {
  11. int offset = 5;
  12. std::vector<int> v;
  13. std::vector<int> w;
  14. for(int i = 1; i <= 2; ++i) v.push_back(i * 10);
  15. BOOST_TEST(v[0] == 10); BOOST_TEST(v[1] == 20);
  16. w.resize(v.size());
  17. int BOOST_LOCAL_FUNCTION( (const bind& offset) (int i) ) {
  18. return ++i + offset;
  19. } BOOST_LOCAL_FUNCTION_NAME(inc)
  20. std::transform(v.begin(), v.end(), w.begin(), inc);
  21. BOOST_TEST(w[0] == 16); BOOST_TEST(w[1] == 26);
  22. int BOOST_LOCAL_FUNCTION( (bind& inc) (int i) (int j) ) {
  23. return inc(i + j);
  24. } BOOST_LOCAL_FUNCTION_NAME(inc_sum)
  25. offset = 0;
  26. std::transform(v.begin(), v.end(), w.begin(), v.begin(), inc_sum);
  27. BOOST_TEST(v[0] == 27); BOOST_TEST(v[1] == 47);
  28. return boost::report_errors();
  29. }