Connecting Tech Pros Worldwide Forums | Help | Site Map

q: STL vector manipulation

laniik
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi. I have a STL vector of some relativly complicated objects

I was wondering if there was a good way to remove objects from the
middle of the vector.

Currently the only way I know how to remove element i is:

1. set the contents of the object at index i to the contents of the
last object
2. vector.pop_back();

but this is obviously annoying if my objects are not just simple
structs.

Thanks!

Oliver


Ben
Guest
 
Posts: n/a
#2: Jul 23 '05

re: q: STL vector manipulation


On 2005-05-26 17:31:10 -0400, "laniik" <lan...@yahoo.com> said:


[color=blue]
> Hi. I have a STL vector of some relativly complicated objects[/color]
[color=blue]
> I was wondering if there was a good way to remove objects from the
> middle of the vector.[/color]



vec.erase(iter);

where vec is your vector, and iter is an iterator to the object you
wish to remove.


--
Clark S. Cox, III
clarkc...@gmail.com


Or you can do vec.erase(vec.begin() + i) // where i is an integer
for the element #

If you are doing a lot of deleting from the middle then you may not
want to use vectors. A list might be better for you, in which case,
you must use the erase that Clark suggested.

-Benjamin Dacko

Ioannis Vranos
Guest
 
Posts: n/a
#3: Jul 23 '05

re: q: STL vector manipulation


Ben wrote:
[color=blue]
> vec.erase(iter);
>
> where vec is your vector, and iter is an iterator to the object you
> wish to remove.
>
>
> --
> Clark S. Cox, III
> clarkc...@gmail.com
>
>
> Or you can do vec.erase(vec.begin() + i) // where i is an integer
> for the element #[/color]


Which is the same, an iterator to the object.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
laniik
Guest
 
Posts: n/a
#4: Jul 23 '05

re: q: STL vector manipulation


great thanks a lot

oliver

Stephen Howe
Guest
 
Posts: n/a
#5: Jul 23 '05

re: q: STL vector manipulation



"laniik" <laniik@yahoo.com> wrote in message
news:1117143070.263798.37950@g49g2000cwa.googlegro ups.com...[color=blue]
> Hi. I have a STL vector of some relativly complicated objects
>
> I was wondering if there was a good way to remove objects from the
> middle of the vector.[/color]

You can do

v.erase(remove(v.begin(), v.end(), value), v.end());

which basically translates to "copy all the non-values towards the beginning
of the vector and erase the crud at the end".

So if you had a vector of int's, and there were 16 5's scattered over the
vector, then

v.erase(remove(v.begin(), v.end(), 5), v.end());

would make 1 pass over the vector, compacting all the non-5's and erasing 16
elements at the end.

Stephen Howe


Ben
Guest
 
Posts: n/a
#6: Jul 23 '05

re: q: STL vector manipulation




Ben wrote:[color=blue]
> vec.erase(iter);[/color]
[color=blue]
> where vec is your vector, and iter is an iterator to the object you
> wish to remove.[/color]

[color=blue]
> --
> Clark S. Cox, III
> clarkc...@gmail.com[/color]

[color=blue]
> Or you can do vec.erase(vec.begin() + i) // where i is an integer
> for the element #[/color]



Which is the same, an iterator to the object.

--
Ioannis Vranos


http://www23.brinkster.com/noi*cys



Yes, it is an iterator.
I just wanted to show laniik how to do an erase using the same index
that he was using in his step 1.

-Benjamin Dacko

Closed Thread