counted_iterator.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-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. // Project home: https://github.com/ericniebler/range-v3
  11. #include <range/v3/iterator/counted_iterator.hpp>
  12. #include "../simple_test.hpp"
  13. // iterator that models input_iterator (has void-returning postfix increment operator)
  14. struct Iterator
  15. {
  16. using value_type = int;
  17. using difference_type = int;
  18. int counter = 0;
  19. int operator*() const { return counter; }
  20. Iterator& operator++() { ++counter; return *this; }
  21. void operator++(int) { ++counter; }
  22. bool operator==(const Iterator& rhs) const { return counter == rhs.counter; }
  23. bool operator!=(const Iterator& rhs) const { return !(*this == rhs); }
  24. };
  25. int main()
  26. {
  27. CPP_assert(ranges::input_iterator<Iterator>);
  28. auto cnt = ranges::counted_iterator<Iterator>(Iterator(), 1);
  29. CHECK(*cnt == 0);
  30. cnt++;
  31. CHECK(cnt == ranges::default_sentinel);
  32. return test_result();
  33. }