for_each_sequence.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Range v3 library
  2. //
  3. // Copyright Jeff Garland 2017
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. //
  12. ///[for_each_sequence]
  13. // Use the for_each to print from various containers
  14. // output
  15. // vector: 1 2 3 4 5 6
  16. // array: 1 2 3 4 5 6
  17. // list: 1 2 3 4 5 6
  18. // fwd_list: 1 2 3 4 5 6
  19. // deque: 1 2 3 4 5 6
  20. #include <array>
  21. #include <deque>
  22. #include <forward_list>
  23. #include <iostream>
  24. #include <list>
  25. #include <queue>
  26. #include <range/v3/algorithm/for_each.hpp> // specific includes
  27. #include <stack>
  28. #include <vector>
  29. using std::cout;
  30. auto print = [](int i) { cout << i << ' '; };
  31. int
  32. main()
  33. {
  34. cout << "vector: ";
  35. std::vector<int> v{1, 2, 3, 4, 5, 6};
  36. ranges::for_each(v, print); // 1 2 3 4 5 6
  37. cout << "\narray: ";
  38. std::array<int, 6> a{1, 2, 3, 4, 5, 6};
  39. ranges::for_each(a, print);
  40. cout << "\nlist: ";
  41. std::list<int> ll{1, 2, 3, 4, 5, 6};
  42. ranges::for_each(ll, print);
  43. cout << "\nfwd_list: ";
  44. std::forward_list<int> fl{1, 2, 3, 4, 5, 6};
  45. ranges::for_each(fl, print);
  46. cout << "\ndeque: ";
  47. std::deque<int> d{1, 2, 3, 4, 5, 6};
  48. ranges::for_each(d, print);
  49. cout << '\n';
  50. }
  51. ///[for_each_sequence]