[C++] Deleting and Copying with std::vector

Started by
5 comments, last by Zahlman 17 years, 2 months ago
I have a loop where I use an iterator to iterate over the elements of a vector. When a certain condition is reached I copy the original vector, then delete two specific elements in the new vector using the iterator I used to iterate over the original vector. ie: copy_of_vector.erase(iterator_from_original_vector); But my code doesnt seem to behave....any ideas?
Advertisement
copying a vector will invalidate the iterators. The iterator will be referring to a specific memory location, not a specific numbered element like, say, 10. So that iterator would be referring to the original vector. What you will need to do is determine the index of the iterator and then you can use newVector.begin() + index to get a new iterator you can erase. I'm not entirely sure how to get the index from an iterator though.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Cannot do. An iterator refers to a specific element of a specific container, not to a specific position in any container. Use std::distance to get the distance between the original vector's begin() and the iterator in the original vector, then add that distance to the begin() of the new vector to get the corresponding position in the new vector.
I'll look into thoes suggestions....but in the mean time I have a followup....how can I replace an element in the vector. I am away from my machine, but I imagine:

"*iter = newValue"


...will work, am I right?
Quote:Original post by fpsgamer
I'll look into thoes suggestions....but in the mean time I have a followup....how can I replace an element in the vector. I am away from my machine, but I imagine:

"*iter = newValue"


...will work, am I right?


That will copy newValue into the object pointed to by the iterator. It won't actually insert newValue into the vector.

For a built in type, this will be a simple assignment. For a class type this will be accomplished with the assignment operator (either the default one or one you provide).
Quote:Original post by EasilyConfused
Quote:Original post by fpsgamer
I'll look into thoes suggestions....but in the mean time I have a followup....how can I replace an element in the vector. I am away from my machine, but I imagine:

"*iter = newValue"


...will work, am I right?


That will copy newValue into the object pointed to by the iterator. It won't actually insert newValue into the vector.

For a built in type, this will be a simple assignment. For a class type this will be accomplished with the assignment operator (either the default one or one you provide).


I just got to try what I was thinking and as you said it doesn't work.

But I am not entirely sure how to fix it.

I have a vector which holds pointers to a class that I have defined. I simply want to replace one of thoes pointers with a new one. How would I go about this?

edit: uhh nevermind, i just had a small logic error :)
What EXACTLY are you trying to do? Give a detailed example.

What is the data type of the things in the vector?

Under what conditions will what things end up in the copy-vector?

Give examples showing sample contents of the original, and what should be in the copy as a result.

This topic is closed to new replies.

Advertisement