473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2337
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[1008000], &ivec[11088000 + 1]);
and clear 'ivec':

std::vector<int>().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
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[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);
Sep 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
3
by: Jonathan | last post by:
Hey again everyone! I have another question for you guys. I am trying to erase a certain vector element based on what number the user selects. Here is what I did: case 'b' : { cout << "Please...
9
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
5
by: Billy Patton | last post by:
I have a polygon loaded into a vector. I need to remove redundant points. Here is an example line segment that shows redundant points a---------b--------c--------d Both b and c are not...
14
by: cayblood | last post by:
I want to iterate through a vector and erase elements that meet a certain criteria. I know there is an algorithmic way of doing this, but first I would like to know how to do it with normal...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
4
by: Christian Bruckhoff | last post by:
Hi. Is there a possibility to delete elements out of a vector? At the moment i do it by copying all needed elements to another vector. After clearing the old vector i copy them back element by...
3
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have some code where there's this vector of pointers to objects and I need to delete and erase some of them, the problem is that to know which I need to iterate through the vector and I'm trying...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
3
by: chsalvia | last post by:
I have a question about the design of STL vector. One thing I wonder was why the STL designers chose to have the insert() and erase() functions take an iterator as the first argument, rather than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.