471,582 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

adding/removing elements from std::vector

I have a std::vector<int> which, after some initialization, has a
fixed number of elements...after initialization I must do the
following repeatedly: I remove an element which could be anywhere in
the vector, and add another element which will always be at the end,
ie.

vector<int> v;
int i, x;

.... initialization

v.erase( v.begin() + i );
v.push_back( x );

My question is whether there is a better way to do this...since I know
that there will always be a sequence of removing an element followed
by appending an element to the end, can I do it in a way which doesn't
use either erase or push_back? Or are most implementations such that
this shouldn't be a problem? Since this erase/push_back sequence will
happen many times, I would like to avoid any possible memory
management since the size of the vector will never really change.

Regards,
Tino
Jul 22 '05 #1
1 2140

"Tino" <ti****@yahoo.com> wrote in message
news:f9**************************@posting.google.c om...
I have a std::vector<int> which, after some initialization, has a
fixed number of elements...after initialization I must do the
following repeatedly: I remove an element which could be anywhere in
the vector, and add another element which will always be at the end,
ie.

vector<int> v;
int i, x;

... initialization

v.erase( v.begin() + i );
v.push_back( x );

My question is whether there is a better way to do this...since I know that there will always be a sequence of removing an element followed
by appending an element to the end, can I do it in a way which doesn't use either erase or push_back? Or are most implementations such that this shouldn't be a problem? Since this erase/push_back sequence will happen many times, I would like to avoid any possible memory
management since the size of the vector will never really change.


Iterators pointing to poisitions before the removed element are
required not to be invalidated by removal, which means that the
removal will not cause any deallocation and reallocation of the
vector's underlying storage. When you immediately append another
element, it should be occupy an already allocated position.

The main expense is the assignments required to effecively shift the
elements above the removal point down one index. This is what you
might want to avoid, perhaps by using a list, depending on your other
requirements.

Jonathan
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jason Heyes | last post: by
17 posts views Thread by Michael Hopkins | last post: by
32 posts views Thread by zl2k | last post: by
56 posts views Thread by Peter Olcott | last post: by
9 posts views Thread by aaragon | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.