vendredi 31 janvier 2020

Custom iterator range function invalid output

I want to implement range function to traverse container

template<class IterT>
struct iteratorRange {
    IterT begin_;
    IterT end_;

    IterT begin() {
        return begin_;
    }

    IterT end() {
        return end_;
    }
};

template<class T>
iteratorRange<typename vector<T>::iterator>Head(vector<T> v, size_t step) {
    return {begin(v), next(begin(v), min(step, v.size()))};
}

int main() {
   vector<int> v {1, 2, 3, 4, 5, 6, 7};
   for (int& i : Head(v, 4)) {
       cout << i  << ' '; 
   }
}

I expected

1 2 3 4

But received

1103219 1109239 3 4

What is the error?

Aucun commentaire:

Enregistrer un commentaire