samedi 22 juillet 2017

C++ iterators how to implement next() and previous() functions

I am writing a utility class that contains a vector of things. I want to provide accessor navigation functions to the caller classes. For example,

class MyIterator {

public:
  typedef std::vector<someObj>::iterator itr;

  itr next() { return things_.begin(); }
  itr next(itr) { return std::next(itr); }

private:
  std::vector<someObj> things_;
}

I have two questions here:

  • Is this this the correct way of doing this, or any simple method available? I am not particularly comfortable with overloading the next() with and without itr.
  • In this situation, how does the caller detect the end of iteration? caller doesn't have access to vector, and hence can't call v.end(). So, should I provide another fn for this check? If so, it looks to be an overkill.

Surely, I am missing something here...

Aucun commentaire:

Enregistrer un commentaire