473,785 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::iterat or 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 2366
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::iterat or 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
--
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.b egin() + end, vec.end());
vec.erase(vec.b egin(), vec.begin() + nstart);

Or, to copy the retained elements:

INT_VEC new_vec(vec.beg in() + 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.********@com Acast.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<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(iv ec);
Sep 25 '07 #4

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

Similar topics

5
10029
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 this an unsafe transfer between heap and stack // or does the allocator handle it safely?
3
1997
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 enter note number to clear: "; cin >> clear_note; clear_note = clear_note - 1; vec.erase(clear_note);
9
3615
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 even numbers. My questions is actullay I have to put pos++ in the body of the loop(also in if, else, statement). Can I somehow put it in for(...;...;...). Or can I use index instead of iterator in order to delete elements from
5
2412
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 necessary. The function below is supposed to remove this It seems to work unitl the last point is removed. It seems to have something to do with the fact that vp->end() is a redundant point. THe test case is code so that "a" is the initial point,...
14
18588
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 iteration. I'm am trying to do something like: vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4);
9
5214
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); vector<double>::iterator i=x.begin()+2, j=x.begin()+6; x.erase(i,j); // i<j, ok, erases 4 elements. x.erase(j,i); // j>i, no error, just inserts 4 elements.
4
2473
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 element. Regards! Christian
3
6192
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 to do this as efficient as possible. The code is something like this: struct Thing { int value; Thing* ptr; Thing() : ptr(0) { } };
10
6077
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 remove the elements with odd values from your list * and the even values from your vector.
3
2889
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 simply an array index integer referring to a position in the array. The reason I wonder this is because firstly, the implementation will need to convert the iterator to an array index integer anyway, because the iterator may become invalidated...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.