fork download
  1. #include <iostream>
  2. #include <cassert>
  3. #include <vector>
  4. #include <type_traits>
  5. using namespace std;
  6.  
  7. template<class C>
  8. struct Iterator{
  9. operator bool() const{
  10. return startiterator != enditerator;
  11. }
  12. const Iterator &operator ++(){
  13. assert(*this);
  14. startiterator++;
  15. return *this;
  16. }
  17. const Iterator operator ++(int){
  18. Iterator other(*this);
  19. startiterator++;
  20. return other;
  21. }
  22. typename C::iterator startiterator, enditerator;
  23. //typename C::iterator sollte durch decltype(C::begin()) oder so ersetz werden
  24. Iterator(C &c) : startiterator(c.begin()), enditerator(c.end()){
  25. }
  26.  
  27. auto operator *() -> decltype(*startiterator){
  28. //hier muss man noch was mit add_const und add_rvalue_reference machen
  29. return *startiterator;
  30. }
  31. };
  32.  
  33. //Am ende den ganzen Spaß nochmal für const-Iterator
  34.  
  35. int main(){
  36. vector<int> v = {42, 37, 128};
  37. Iterator<vector<int>> it(v); //kann man noch in eine makeIterator-Funktion tun
  38. while (it){
  39. cout << *it << ' ';
  40. it++;
  41. }
  42. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
42 37 128