vector<..> question
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
no, it will not leave a hole. BTW, I think you should be calling it like this:
Hope that helps!
-David
XYZ.erase(&XYZ[56]);
Hope that helps!
-David
Oh that sounds very good )
Yes should be XYZ.erase(XYZ[56]);.
It was a typemistake.
Thanks, Austrian Coder
Yes should be XYZ.erase(XYZ[56]);.
It was a typemistake.
Thanks, Austrian Coder
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement