filtered_example.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. //[filtered_example
  12. #include <boost/range/adaptor/filtered.hpp>
  13. #include <boost/range/algorithm/copy.hpp>
  14. #include <boost/assign.hpp>
  15. #include <iterator>
  16. #include <iostream>
  17. #include <vector>
  18. //<-
  19. #include <boost/test/test_tools.hpp>
  20. #include <boost/test/unit_test.hpp>
  21. #include <boost/range/algorithm_ext/push_back.hpp>
  22. namespace
  23. {
  24. //->
  25. struct is_even
  26. {
  27. bool operator()( int x ) const { return x % 2 == 0; }
  28. };
  29. //<-
  30. void filtered_example_test()
  31. //->
  32. //=int main(int argc, const char* argv[])
  33. {
  34. using namespace boost::assign;
  35. using namespace boost::adaptors;
  36. std::vector<int> input;
  37. input += 1,2,3,4,5,6,7,8,9;
  38. boost::copy(
  39. input | filtered(is_even()),
  40. std::ostream_iterator<int>(std::cout, ","));
  41. //= return 0;
  42. //=}
  43. //]
  44. std::vector<int> reference;
  45. reference += 2,4,6,8;
  46. std::vector<int> test;
  47. boost::push_back(test, input | filtered(is_even()));
  48. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  49. test.begin(), test.end() );
  50. }
  51. }
  52. boost::unit_test::test_suite*
  53. init_unit_test_suite(int argc, char* argv[])
  54. {
  55. boost::unit_test::test_suite* test
  56. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.filtered_example" );
  57. test->add( BOOST_TEST_CASE( &filtered_example_test ) );
  58. return test;
  59. }