stl vector iterators

Started by
6 comments, last by Thunder_Hawk 19 years, 8 months ago
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.
Advertisement
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.
www.tmreality.com
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.
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;    vector<int>::iterator it = v.begin()+2;    cout << (*it) << endl;    it = v.erase(it);    cout << (*it) << endl;    return 0;}


prints 2 then 3
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?
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() );
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement