[RESOLVED] removing an element from a vector?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: [RESOLVED] removing an element from a vector?

  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    [RESOLVED] removing an element from a vector?

    Hello,

    Say I have a vector of something called "vec" And I use this for loop to access the elements of vec

    for(size_t i=0; i<vec.size(); i++)
    {
    }

    How can I remove the element from the vector without an iterator?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: removing an element from a vector?

    You can get an iterator from a vector's index by adding the index to the iterator returned by begin(). That said, have you considered using std::remove() or std::remove_if() with the version of erase() that takes iterators that denote a range? That approach is less error prone and likely to be faster.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2001
    Posts
    1,029

    Re: removing an element from a vector?

    Hello Laserlight..

    So to remove I might do this:

    vec.erase(vec.begin()+i); ?

    Is that right??

    Thanks!

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646

    Re: removing an element from a vector?

    Yes, but you need to be careful about the loop index variable "i"
    if you erase an element.

  5. #5
    Join Date
    Apr 2001
    Posts
    1,029

    Re: removing an element from a vector?

    when will this code not work then? I guess what you mean is that i will actually change right?

    say I have these elements in the list: a a b c d

    and I do an erase on b, which is index i=2 my code would look like this:

    vec.erase(vec.begin()+i);

    however, in my for loop if it continues it will next grab index=3 which is now d, skipping the element c.

    Is that a correct analysis?

    Thanks!

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: removing an element from a vector?

    Yes, unless you make it such that you only increment the loop index when you do not erase anything. So, why not use std::remove() or std::remove_if()?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 2001
    Posts
    1,029

    Re: removing an element from a vector?

    hey laser,

    maybe I will look at those methods. I break form the loop when I find the element I want to erase, so the loop is not an issue.

    Thanks though!!!

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: [RESOLVED] removing an element from a vector?

    Iterators could be more flexible than an index as erase returns an iterator to the element after the last element erased.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: [RESOLVED] removing an element from a vector?

    Quote Originally Posted by lab1
    I break form the loop when I find the element I want to erase, so the loop is not an issue.
    In that case, use std::find() or std::find_if() with erase().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Apr 2001
    Posts
    1,029

    Re: [RESOLVED] removing an element from a vector?

    Heya laser,

    I would have done find() but the list is of sockaddr_in's and all I have is an ip address and a port. so I have to go through each sockaddr_in and compare those internal values.

    Thanks though!

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: [RESOLVED] removing an element from a vector?

    Quote Originally Posted by lab1
    I would have done find() but the list is of sockaddr_in's and all I have is an ip address and a port. so I have to go through each sockaddr_in and compare those internal values.
    That is where std::find_if() would come in handy.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: [RESOLVED] removing an element from a vector?

    Quote Originally Posted by laserlight View Post
    That is where std::find_if() would come in handy.
    I second that! Just write a predicate functor and use find_if.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  13. #13
    Join Date
    May 2007
    Posts
    800

    Re: [RESOLVED] removing an element from a vector?

    Not exactly to original question but when removing elements from std::vector, always keep this in mind.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center