• Popular Now

• 9
• 9
• 11
• 12
• 9

Archived

This topic is now archived and is closed to further replies.

vector<..> question

This topic is 5773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello! I have here a little problem. I have an array of a struct (vector XYZ) and now i have filled it with datas. So e.g. I have 502 Items in it and if i want to delete the # 56 what is the best way to do this? Will there be an hole? Here source: XYZ.erase(XYZ[56]); Is there now a hole? When yes how could i fix it? thx, Austrian Coder

Share on other sites
no, it will not leave a hole. BTW, I think you should be calling it like this:
XYZ.erase(&XYZ[56]);

Hope that helps!

-David

Share on other sites
Oh that sounds very good )
Yes should be XYZ.erase(XYZ[56]);.
It was a typemistake.

Thanks, Austrian Coder

Share on other sites
quote:
Original post by Austrian Coder
I have 502 Items in it and if i want to delete the # 56 what is the best way to do this? Will there be an hole?

There won''t be a hole, but there''ll be a lot of copying! The vector will "cover up" that hole by shifting every element from #57 to #502 back one space - 445 copies!

If the order of the elements is not important, then swap #56 with the last element (#502) and delete from the end. If order is important and you need to do this kind of deleting from the middle frequently, use a list. Always determine what kind of data structure to use by your requirements: for fast access to random elements, use a vector; for fast insertion and deletion at random locations, use a list; etc.