adding/removing elements from std::vector

Discussion in 'C++' started by Tino, Feb 20, 2004.

  1. Tino

    Tino Guest

    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
     
    Tino, Feb 20, 2004
    #1
    1. Advertising

  2. "Tino" <> wrote in message
    news:...
    > 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
     
    Jonathan Turkanis, Feb 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Heyes
    Replies:
    6
    Views:
    8,395
    Jason Heyes
    Nov 16, 2004
  2. Anonymous
    Replies:
    20
    Views:
    4,358
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    742
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,970
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    965
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page