mercredi 29 novembre 2017

Custom iterator function for c++

I have a collection of object of type "T" that i want to iterate through. An object of type "T" has two important properties:

int r; // row number int c; // column number

I would like to define an iterator that allows me to iterate through all elements of the collection.

This can be done using:

std::vector v;

for(std::vector::iterator it = v.begin(); it != v.end(); ++it) { .... }

However, I would like the iterator to have one more property. I would like to be able to call

it.nextrow()

calling this function should return the element "e" of v where e.r + 1 = ec.r and e.c = ec.c, where ec is the current element pointed by the iterator. I.e. calling it.nextrow() should give me a pointer to the element where column is the same, but row is incremented by one. Hope it makes sense.

I am not sure what I need to do in order for this to work, as I am fairly new advanced c++ concepts. Can anybody help me?

Aucun commentaire:

Enregistrer un commentaire