Jump to content
  • Advertisement
Sign in to follow this  
Dasil

Vector Iterators

This topic is 3093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi again, I'm trying to learn more about data structures, so I'm writing my own custom versions of each over time. Right now I'm working on vectors, and I've run into a problem with iterators. I've never used them with vectors before (I've always used []), so I'm wondering what information it would contain. I've used list iterators before, so is it something similar to that? Thanks

Share this post


Link to post
Share on other sites
Advertisement
An iterator to a vector would behave exactly the same way as a pointer into the internal array. The only information they need to contain is a single pointer to an element in the vector. It needs to overload all operators that work with pointers.

Share this post


Link to post
Share on other sites
Expanding on visitor's post:
class MyVector
{
public:
T* begin() { return buffer; }
T* end() { return buffer + size; }
private:
T* buffer;
int size;
};

Share this post


Link to post
Share on other sites
Of course, the iterator needn't be just a pointer (there might be advantages to it being a unique class). For example, GCC's vector iterator is a __normal_iterator<T, vector<T> >.

Also make sure to inherit from std::iterator with suitable template parameters. For example:


template <class T>
class MyVectorIterator: public std::iterator<std::random_access_iterator_tag, T>
{
//...
};

template <class T>
class MyLinkedListIterator: public std::iterator<std::bidirectional_iterator_tag, T>
{
//...
};




(or provide the required typedefs) or else your iterators may not work well with the rest of the STL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!