-
February 27th, 2009, 08:23 AM
#1
[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!
-
February 27th, 2009, 08:30 AM
#2
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.
-
February 27th, 2009, 09:00 AM
#3
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!
-
February 27th, 2009, 09:05 AM
#4
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.
-
February 27th, 2009, 09:33 AM
#5
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!
-
February 27th, 2009, 09:38 AM
#6
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()?
-
February 27th, 2009, 09:45 AM
#7
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!!!
-
February 27th, 2009, 09:51 AM
#8
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
-
February 27th, 2009, 09:58 AM
#9
Re: [RESOLVED] removing an element from a vector?
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().
-
February 27th, 2009, 10:08 AM
#10
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!
-
February 27th, 2009, 10:10 AM
#11
Re: [RESOLVED] removing an element from a vector?
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.
-
February 27th, 2009, 10:31 AM
#12
Re: [RESOLVED] removing an element from a vector?
Originally Posted by laserlight
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
-
February 27th, 2009, 11:17 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|