I have situation where i have lots of objects in one container and i need to erase some element/s from arbitary position,
but that can envolve lots of copying being made, so i thought i should try to swap object from that position with last one
and erase that one. First question, is there some common function for this one?
Currently i have wrote this:
Hmm. Maybe you could setup a vector of pairs which contain the values and the original subscripts of all the data, and after you're done erasing everything that needs to be erased you could just sort by the original subscripts.
What he's trying to avoid is a vector's need to shift all the data after the erased element to maintain an array-like structure. Maybe he has a good reason for that.
Although, why not use an std::deque? It has some higher constant factors, true, but its erase() does seem to me to be a bit more efficient than an std::vector's, albeit not as efficient as an std::list's (though lists have some efficiency problems in other areas...).
What built in? std::vector::erase() ? But what if i invoke that on 0 inx of vector of 5000 elements that would invoke almost 5000 copyies just to shift elements?
I would like (if possible) after erase if elements can remain sorted in few cheap steps, so that i don't need to call sort immideatly after.
If you have some ideas please share.
Thanks greatly on your time.
But what if i invoke that on 0 inx of vector of 5000 elements that would invoke almost 5000 copyies just to shift elements?
Yeah, but if you want it to stay sorted, that's what it takes.
The alternative would be to use something like deque as Albatross suggested. It offers [potentially] faster insertion/removal with a tradeoff of [potentially] slower data access.
Or if you don't need random access you could use a list instead.
I would like (if possible) after erase if elements can remain sorted in few cheap steps
If such a thing were possible, vector::erase would do it. The people that wrote those libs knew what they were doing.