Removing element from vector?

This is a discussion on Removing element from vector? within the C++ Programming forums, part of the General Programming Boards category; How do I remove an element from a vector? For example, removing the 3 from a vector containing 1,2,3,4 so ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Removing element from vector?

    How do I remove an element from a vector? For example, removing the 3 from a vector containing 1,2,3,4 so it becomes 1,2,4

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Call std::vector.erase with the index you want to erase, in case the 3rd index:
    Code:
    std::vector myvec;
    // Assume vector is filled with data
    myvec.erase(myvec.begin() + 2);
    Or if you have an iterator, that should work too.
    Last edited by Elysia; 02-03-2008 at 11:24 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    or if you want to remove all elements equal to 3 so that 1,3,2,3,4,3 becomes 1,2,4 use the erase-remove idiom:
    Code:
    v.erase( remove( v.begin(), v.end(), 3 ), v.end() );

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by Elysia View Post
    Call std::vector.erase with the index you want to erase, in case the 3rd index:
    Code:
    std::vector myvec;
    // Assume vector is filled with data
    myvec.erase(myvec.begin() + 3);
    Or if you have an iterator, that should work too.
    Actually, that's +2, not +3, for erasing the third item.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Right, silly me. 0-based index.
    /slaps self.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    note that vector's erase takes linear time (proportional to the size of the dataset). If you are doing this relatively often, you should be using something like linked lists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21