Sign in to follow this  
Dasil

Vector Iterators

Recommended Posts

Dasil    190
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
visitor    643
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
visitor    643
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this