Say I have a vector with 100000 elements, and I wish to iterate through the vector, one by one, whilst copying the element into a map of some sort, but during each iteration, deleting the element from the vector - what is the most efficient way to do this?
Whilst iterating through the vector, I had done something like "it = vec.erase(it)" but it takes absolutely ages to complete. Is there not a quicker way? And as a side note, ordering is very important...
There isn't one. You are using the vector as a queue. This goes against its design.
You have a few choices. The following come immediately to mind:
Don't erase one item at a time. Do them in batches.
Use the vector as a ring buffer and just advance an index, but never remove elements.
Use a more appropriate container such as std::deque
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With