• Advertisement
Sign in to follow this  

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

This topic is 4045 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

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


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

  • Advertisement