Connecting Tech Pros Worldwide Help | Site Map

q: STL vector manipulation

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:51 AM
laniik
Guest
 
Posts: n/a
Default q: STL vector manipulation

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


  #2  
Old July 23rd, 2005, 04:51 AM
Ben
Guest
 
Posts: n/a
Default 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

  #3  
Old July 23rd, 2005, 04:51 AM
Ioannis Vranos
Guest
 
Posts: n/a
Default 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
  #4  
Old July 23rd, 2005, 04:51 AM
laniik
Guest
 
Posts: n/a
Default Re: q: STL vector manipulation

great thanks a lot

oliver

  #5  
Old July 23rd, 2005, 04:51 AM
Stephen Howe
Guest
 
Posts: n/a
Default Re: 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


  #6  
Old July 23rd, 2005, 04:51 AM
Ben
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.