Removing an element from a vector

Started by
16 comments, last by emileej 20 years, 5 months ago
I need to remove element #idx from vector v. How is this done? Like this? v.erase(&v.front()+idx); "On a long enough timeline the survival rate of everyone drops to zero" - Fight Club
Emil Johansen- SMMOG AI designerhttp://smmog.com
Advertisement
yes except without the address & in the front




--{You fight like a dairy farmer!}

--{You fight like a dairy farmer!}

as far as i know the first item is returned by vector::begin()

v.erase( v.begin() + idx );

our new version has many new and good features. sadly, the good ones are not new and the new ones are not good
error C2678: binary ''+'' : no operator defined which takes a left-hand operand of type ''struct voter'' (or there is no acceptable conversion)
Emil Johansen- SMMOG AI designerhttp://smmog.com
quote:Original post by emileej
I need to remove element #idx from vector v. How is this done? Like this? v.erase(&v.front()+idx);
It doesn''t work?
what is the type of idx? integer or size_type?
Well, R2D22U2..
integer
Emil Johansen- SMMOG AI designerhttp://smmog.com
Google says...
Anyway, from what I can see, really, unless your standard library is really weird, v.erase(&v.front()+idx) should work. Again, did you try it?

And, AFAIK, it is equivalent to &v.front() == v.begin() since vector iterators are pointers.

Cédric
It should work but there appears to be some sort of detail that is missing...you may need to post some of the code.
Well, R2D22U2..
You can use operator [] with the std::vector too

v.erase(&v[index]);

Although you should check that the index is a valid number

This topic is closed to new replies.

Advertisement