Sign in to follow this  
bobster

stl vector iterators

Recommended Posts

Hi all, one quick question about stl iterators for the vector class. You see everywhere ppl describing them as being used in the same way as pointers, however I can't find anywhere a clear explanation of whether they point to a vector index or the object itself in a particular position in the vector. To be a little clearer what I mean is: Say I were to declare a iterator that pointed to a particular object in a vector, say at position 5 in a vector of size 10. If I erase() the object at position 2 (thereby now making the vector of size 9) will the iterator I declared before now point at the original object which is now at position 4 or will it continue to point at position 5, and the new object stored at that location? Thanks in advance for any replies.

Share this post


Link to post
Share on other sites
Vector iterator become invalid when you make operations on vectors. Treat them as pointers and use logic - vector is just wrapped new[]ed array. Even if you are inseting at the end of the vector whole array could be realloctaed\, therefore always assume that changing a vector invalidates its iterators

EDIT: It will probably point to 5th object in the vector after erase(), but you shouldn't really assume that. As someone once replied me: iterators aren't meant to be stored and used for a long time - just 'in place' to iterate thru you container. Doing this way you will never have problems.

Share this post


Link to post
Share on other sites
It would be undefined. Calling erase() on a vector invalidates all iterators to elements after the erase(). Depending on your STL implementation it could either point to the fourth or the fifth element or it might reformat your hard drive and pipe the "Star Spangled Banner" over all radios within a five block radius.

Share this post


Link to post
Share on other sites
STL functions that modify the contents all return an iterator since there is no guarantee that any existing pointers into the container are still valid (for example it could have moved the entire block of memory used to store the vector).

vector::erase returns the first element not deleted

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> v(5);
for(int i=0; i < 5; ++i)
v[i] = i;

vector<int>::iterator it = v.begin()+2;
cout << (*it) << endl;
it = v.erase(it);
cout << (*it) << endl;
return 0;
}



prints 2 then 3

Share this post


Link to post
Share on other sites
thanks for the (as ever!) extremely quick replies. This leads me on to the problem I'm having:
If iterators arent designed to be used beyond cycling through a vector, can anyone come up with a suggestion as how to mark out or group particular objects? I have a vector of sprites that are fed into the engine I have created, and I am currently adding a menu system. I want to add the menu sprites to the vector but when I wish to deactivate the menu I want to be able to remove all these menu sprites from the sprites vector with a single function. How can I keep track of these menu sprites to erase them when there are constantly new sprites being added to and subtracted from the vector?

Share this post


Link to post
Share on other sites
std::remove_if lets you pass a predicate identifying the elements that are to be removed from the container.
Note that remove-like algorithms are not safe to use on containers of (dumb) pointers.

Idiom: vec.erase( remove_if( vec.begin(), vec.end(), Predicate ), vec.end() );

Share this post


Link to post
Share on other sites
Don't forget that you can use STL containers inside of other STL containers, too. For example, if you want to group the sprites in some manner, but still have them all in one container, you can use a std::map that contains an integer key mapping to a std::vector of sprites. Then when you want to get rid of a certain group, you just look up the key for that group in the map and call erase on the vector stored with that key.

Share this post


Link to post
Share on other sites
You might want to consider using an stl::list instead of a vector here because it's iterators are guaranteed to remain valid until the elements they point to are erased from the list

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