"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fd**********@news.datemas.de...
ma740988 wrote:
>The question. What's the _fastest_ way to erase element numbers less
than 1008000 and element numbers greater than 11088000. Current
approach.
Extract the numbers you want to keep into the separate vector:
std::vector<inttokeep(&ivec[1008000], &ivec[11088000 + 1]);
and clear 'ivec':
std::vector<int>().swap(ivec);
This approach works, but has the disadvantage of requiring enough memory to
hold two copies of all the elements that are retained, along with the rest
of ivec's original contents.
Instead, how about the following?
ivec.erase(ivec.begin() + 11088000+1, ivec.end());
ivec.erase(ivec.begin(), ivec.begin() + 1008000);
This code copies the elements that are retained, but does so into memory
that is already allocated so there is no additional space overhead.
It is true that after it is done, the capacity of ivec is still what it was
originally, but that can't be helped directly. If you care, you can copy
the remaining elements of ivec into new space and free the original:
std::vector<int>(ivec).swap(ivec);