Vector Erase

M

ma740988

For discussion purposes, assume the vector ivec contains 67108864 (67
million elements) elements. Lets futher assume nstart and end equal
1008000 and 11088000 respectively.

The question. What's the _fastest_ way to erase element numbers less
than 1008000 and element numbers greater than 11088000. Current
approach.

typedef std::vector < int > INT_VEC ;

int main () {

int dummy( 0 ) ;
for ( INT_VEC::iterator it = ivec.begin(); it != ivec.end(); ) {
if ( dummy < nstart || dummy > end ) {
it = ivec.erase ( it ) ;
} else {
++ it ;
}
++ dummy ;
}

}

This is 'dog' slow. That said, I thinking it would be ideal if i
copied elements into a separate vector.
 
V

Victor Bazarov

ma740988 said:
For discussion purposes, assume the vector ivec contains 67108864 (67
million elements) elements. Lets futher assume nstart and end equal
1008000 and 11088000 respectively.

The question. What's the _fastest_ way to erase element numbers less
than 1008000 and element numbers greater than 11088000. Current
approach.

typedef std::vector < int > INT_VEC ;

int main () {

int dummy( 0 ) ;
for ( INT_VEC::iterator it = ivec.begin(); it != ivec.end(); ) {
if ( dummy < nstart || dummy > end ) {
it = ivec.erase ( it ) ;
} else {
++ it ;
}
++ dummy ;
}

}

This is 'dog' slow. That said, I thinking it would be ideal if i
copied elements into a separate vector.

Extract the numbers you want to keep into the separate vector:

std::vector<int> tokeep(&ivec[1008000], &ivec[11088000 + 1]);


and clear 'ivec':

std::vector<int>().swap(ivec);

V
 
P

Pete Becker

This is 'dog' slow.

Of course it is. Each time the code removes an element it copies all
the elements above that one down one position. You end up with an awful
lot of copying.
That said, I thinking it would be ideal if i
copied elements into a separate vector.

Maybe. Or maybe just erase all the tail elements at one time, then all
the head elements at one time.

vec.erase(vec.begin() + end, vec.end());
vec.erase(vec.begin(), vec.begin() + nstart);

Or, to copy the retained elements:

INT_VEC new_vec(vec.begin() + nstart, vec.begin() + end);
 
A

Andrew Koenig

Victor Bazarov said:
ma740988 wrote:
Extract the numbers you want to keep into the separate vector:

std::vector<int> tokeep(&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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top