449,402 Members | 1,107 Online Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

# Vector Erase

 P: n/a 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 Replies

 P: n/a 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().swap(ivec); V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask Sep 24 '07 #2

 P: n/a On 2007-09-24 11:44:40 -0400, ma740988 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

 P: n/a "Victor Bazarov" The question. What's the _fastest_ way to erase element numbers lessthan 1008000 and element numbers greater than 11088000. Currentapproach. Extract the numbers you want to keep into the separate vector: std::vector().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(ivec).swap(ivec); Sep 25 '07 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 