How can I remove an item in STL iterator?

Discussion in 'C++' started by Allerdyce.John@gmail.com, Mar 5, 2006.

  1. Guest

    How can I emove an item in STL iterator without use the STL algorithm?
    I know we can use stl erase, find_if to remove items from a STL vector
    , but how can I do the same without using STL algorithm?

    vector<int> srcVector;
    vector<int> destVector;

    for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) {
    int i = (*itr);

    // if i contains in destVector, remove that from both srcVector
    and destVector

    }
    , Mar 5, 2006
    #1
    1. Advertising

  2. TB Guest

    skrev:
    > How can I emove an item in STL iterator without use the STL algorithm?
    > I know we can use stl erase, find_if to remove items from a STL vector
    > , but how can I do the same without using STL algorithm?
    >
    > vector<int> srcVector;
    > vector<int> destVector;
    >
    > for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) {
    > int i = (*itr);
    >
    > // if i contains in destVector, remove that from both srcVector
    > and destVector
    >
    > }
    >


    std::vector<int> v(10);
    v.erase(v.begin() + 4); // deletes 5th element

    --
    TB @ SWEDEN
    TB, Mar 5, 2006
    #2
    1. Advertising

  3. On 5 Mar 2006 09:20:48 -0800, in comp.lang.c++ you wrote:
    >How can I emove an item in STL iterator without use the STL algorithm?
    >I know we can use stl erase, find_if to remove items from a STL vector
    >, but how can I do the same without using STL algorithm?
    >
    >vector<int> srcVector;
    >vector<int> destVector;
    >
    >for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) {
    > int i = (*itr);
    >
    > // if i contains in destVector, remove that from both srcVector
    >and destVector



    for (vector<int>::iterator itr = srcVector.begin();
    itr != srcVector.end(); ) {

    vector<int>::iterator posDest
    = std::find (destVector.begin(), destVector.end(), *itr)

    if (posDest != destVector.end()) {
    srcVector.remove (itr);
    destVector.remove (posDest);
    } else {
    ++itr;
    }
    }

    Not tested. Does not work for duplicate entries in srcVector or
    destVector. Replace 'std::find' with your own 'find' function if you
    don't want std::algorithms.

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Mar 5, 2006
    #3
  4. Guest

    Thanks. My question is: "Is it save to call remove of a STL container
    as you are iterator thru the same STL container ? " Like the example
    above posting.
    , Mar 5, 2006
    #4
  5. On 5 Mar 2006 11:45:16 -0800, wrote:

    >Thanks. My question is: "Is it save to call remove of a STL container
    >as you are iterator thru the same STL container ? " Like the example
    >above posting.


    Actually the right remove function is erase, e.g,
    srcVector.erase (itr);

    WRT to your question: "iterators and references become invalid only
    from the first element erased through the end of the sequence"
    http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=vector.html#vector::erase

    Best wishes,
    Roland Pibigner
    Roland Pibinger, Mar 5, 2006
    #5
  6. On Sun, 05 Mar 2006 20:56:32 GMT, (Roland Pibinger)
    wrote:

    >WRT to your question: "iterators and references become invalid only
    >from the first element erased through the end of the sequence"
    >http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=vector.html#vector::erase


    After having read that sentence again I see that my solution is
    probably not valid.

    Sorry for the confusion. STL is too complicated for a quick answer.
    Roland Pibinger
    Roland Pibinger, Mar 5, 2006
    #6
  7. Acer Guest

    codes below are SGI STL sourcecode for vector<T>::erase(iterator
    position);

    they are inline in .h

    iterator erase(iterator position)
    {
    if( position+1 != end() )
    copy( position+1, finish, position);
    //u can find this function's definition in <algorithm>
    --finish;
    destory(finish); //<memory>; part of the allocator
    return position;
    }

    Because finish's type is vector<T>::iterator, and actually is T*.
    Therefore, destory will call finish->~T(); to deconstruct item.
    Acer, Mar 6, 2006
    #7
  8. On Sun, 05 Mar 2006 18:33:10 GMT, (Roland Pibinger)
    wrote:
    > if (posDest != destVector.end()) {
    > srcVector.remove (itr);
    > destVector.remove (posDest);
    > } else {
    > ++itr;
    > }
    >}


    The correct code is:

    if (posDest != destVector.end()) {
    itr = srcVector.erase (itr);
    destVector.erase (posDest);
    } else {
    ++itr;
    }
    }
    Roland Pibinger, Mar 6, 2006
    #8
    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. Gunnar G

    remove an item in a STL list

    Gunnar G, Feb 8, 2005, in forum: C++
    Replies:
    9
    Views:
    636
    Pete Becker
    Feb 9, 2005
  2. sam
    Replies:
    12
    Views:
    9,560
    Mike Wahler
    May 12, 2005
  3. forester
    Replies:
    16
    Views:
    709
    Howard Hinnant
    Aug 25, 2005
  4. Jonathan
    Replies:
    6
    Views:
    406
    Jonathan
    Mar 14, 2006
  5. Pranav
    Replies:
    3
    Views:
    690
    ManicQin
    Aug 8, 2008
Loading...

Share This Page