tokenized_example.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. //[tokenized_example
  12. #include <boost/range/adaptor/tokenized.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. void tokenized_example_test()
  25. //->
  26. //=int main(int argc, const char* argv[])
  27. {
  28. using namespace boost::adaptors;
  29. typedef boost::sub_match< std::string::iterator > match_type;
  30. std::string input = " a b c d e f g hijklmnopqrstuvwxyz";
  31. boost::copy(
  32. input | tokenized(boost::regex("\\w+")),
  33. std::ostream_iterator<match_type>(std::cout, "\n"));
  34. //= return 0;
  35. //=}
  36. //]
  37. using namespace boost::assign;
  38. std::vector<std::string> reference;
  39. reference += "a","b","c","d","e","f","g","hijklmnopqrstuvwxyz";
  40. std::vector<match_type> test;
  41. boost::push_back(test, input | tokenized(boost::regex("\\w+")));
  42. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  43. test.begin(), test.end() );
  44. }
  45. }
  46. boost::unit_test::test_suite*
  47. init_unit_test_suite(int argc, char* argv[])
  48. {
  49. boost::unit_test::test_suite* test
  50. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.tokenized_example" );
  51. test->add( BOOST_TEST_CASE( &tokenized_example_test ) );
  52. return test;
  53. }