Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about the stl erase-remove idiom

Reply
Thread Tools

question about the stl erase-remove idiom

 
 
Nan Li
Guest
Posts: n/a
 
      11-08-2005
Hello,
I have seen people doing erase remove idiom like the following:

' vec.erase(remove_if(vec.begin(), vec.end(), is_odd<int>),
vec.end() );'

I remember Scott Meyers also uses this form in his 'Effective
STL' book. ( I cannot verify it right now because the book is not with
me )
But I was wondering if this form is safe, in the strictest sense.
The underlying question is ' does remove guarantee that the end()
iterator is still valid after the operation' ? If it is not guaranteed
and if the second parameter 'vec.end()' is evaluated first (before
remove), then vec.erase will possibly get a new end as the first
parameter, but an old/invalid end() iterator as the second.
I hope I explained my question clearly.

Thanks,
Nan

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-08-2005
Nan Li wrote:
> Hello,
> I have seen people doing erase remove idiom like the following:
>
> ' vec.erase(remove_if(vec.begin(), vec.end(), is_odd<int>),
> vec.end() );'
>
> I remember Scott Meyers also uses this form in his 'Effective
> STL' book. ( I cannot verify it right now because the book is not with
> me )
> But I was wondering if this form is safe, in the strictest sense.
> The underlying question is ' does remove guarantee that the end()
> iterator is still valid after the operation' ? If it is not guaranteed
> and if the second parameter 'vec.end()' is evaluated first (before
> remove), then vec.erase will possibly get a new end as the first
> parameter, but an old/invalid end() iterator as the second.
> I hope I explained my question clearly.


There is no guarantee on the order of execution, but none is required.
The moving around of items in the sequence can't do anything that
would invalidate the end() iterator since it's not evaluated at all
during the execution of remove (it's passed in as an argument).
This is one of the reasons hy remove doesn't actually remove anything.
 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      11-08-2005
"Ron Natalie" <(E-Mail Removed)> wrote in message
news:43711727$0$9524$(E-Mail Removed) ...
> Nan Li wrote:


>> But I was wondering if this form is safe, in the strictest sense.
>> The underlying question is ' does remove guarantee that the end()
>> iterator is still valid after the operation' ?


> There is no guarantee on the order of execution, but none is required.
> The moving around of items in the sequence can't do anything that
> would invalidate the end() iterator since it's not evaluated at all
> during the execution of remove (it's passed in as an argument).


This is a good one to remember the next time you're looking for an interview
question


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault on stl::resize / stl::clear Steve C++ 2 11-06-2007 06:53 AM
stl questions: how can I compare 2 stl list? silverburgh.meryl@gmail.com C++ 5 04-16-2006 09:57 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments