fork download
  1. #include <iostream>
  2. #include <iterator>
  3.  
  4. struct csforward_iterator :
  5. std::iterator<std::bidirectional_iterator_tag, const char, void> {
  6.  
  7. csforward_iterator( pointer ptr = nullptr ) : p( ptr ) {}
  8.  
  9. csforward_iterator& operator++() { ++p; return *this; }
  10. csforward_iterator operator++(int) { auto t = *this; ++p; return t; }
  11.  
  12. csforward_iterator& operator--() { --p; return *this; }
  13. csforward_iterator operator--(int) { auto t = *this; --p; return t; }
  14.  
  15. bool operator==( csforward_iterator o ) {
  16. return p == o.p or ( p ? not ( o.p or *p ) : *o.p );
  17. }
  18. bool operator!=( csforward_iterator o ) { return not operator==( o ); }
  19.  
  20. reference operator*() const { return *p; }
  21. private:
  22. pointer p;
  23. };
  24.  
  25.  
  26. int main() {
  27. std::copy( csforward_iterator( "foobar" ), csforward_iterator(), std::ostream_iterator<char>(std::cout) );
  28. std::cout << std::endl;
  29. }
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
foobar