Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > adding/removing elements from std::vector

Reply
Thread Tools

adding/removing elements from std::vector

 
 
Tino
Guest
Posts: n/a
 
      02-20-2004
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
 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      02-20-2004

"Tino" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
How do I allow both elements or No elements Billy XML 4 09-12-2005 08:29 AM
Elements within elements Jyrki Keisala XML 5 06-15-2005 04:58 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 0 01-28-2004 02:50 PM



Advertisments