example.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2013-present
  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. #include <range/v3/all.hpp>
  11. #include <iostream>
  12. using namespace ranges;
  13. // A range that iterates over all the characters in a
  14. // null-terminated string.
  15. class c_string_range
  16. : public view_facade<c_string_range>
  17. {
  18. friend range_access;
  19. char const * sz_;
  20. char const & read() const { return *sz_; }
  21. bool equal(default_sentinel_t) const { return *sz_ == '\0'; }
  22. void next() { ++sz_; }
  23. public:
  24. c_string_range() = default;
  25. explicit c_string_range(char const *sz) : sz_(sz)
  26. {
  27. assert(sz != nullptr);
  28. }
  29. };
  30. int main()
  31. {
  32. c_string_range r("hello world");
  33. // Iterate over all the characters and print them out
  34. ranges::for_each(r, [](char ch){
  35. std::cout << ch << ' ';
  36. });
  37. // prints: h e l l o w o r l d
  38. }