469,338 Members | 8,430 Online

# Vector Erase

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.

Sep 24 '07 #1
3 2060 ma740988 wrote:
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<inttokeep(&ivec, &ivec[11088000 + 1]);
and clear 'ivec':

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

V
--
Sep 24 '07 #2
On 2007-09-24 11:44:40 -0400, ma740988 <ma******@gmail.comsaid:
>
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);

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 24 '07 #3
"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, &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.

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

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);
Sep 25 '07 #4

### This discussion thread is closed

Replies have been disabled for this discussion.