#### Archived

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

# vector<..> question

This topic is 5863 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.

##### Share on other sites
Thanks for this good tipp. In my current problem the order isnt importent so i will use vector.

thx, Austrian Coder

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 32
• 41
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631742
• Total Posts
3001989
×