fork download
  1. #include <iostream>
  2. #include <iterator>
  3.  
  4. template<class B,class E> struct range_iter{
  5. B b; E e;
  6. range_iter(B b_,E e_):b(b_),e(e_){}
  7. B& begin(){return b;} E& end(){return e;}
  8. range_iter<B,E>& operator++(){++b; return *this;}
  9. typename std::iterator_traits<B>::reference operator*(){return *b;}
  10. bool has_next(){return b!=e;}
  11. bool is_last(){B b2(b); return ++b2==e;}
  12. };
  13. template<class B,class E> range_iter<B,E> to_ri(B b, E e){return range_iter<B,E>(b,e);}
  14.  
  15. int main(){
  16. int a[] = {0,10,20,30,40};
  17. for(auto it=to_ri(a+1, a+3); it.has_next(); ++it){std::cout<<*it<<(it.is_last()?"(last)":"")<<std::endl;}
  18. //for(auto e : to_ri(a+1, a+3)){...}
  19. return 0;
  20. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
10
20(last)