Now that we're on the subject of stl algorithms, I have this question: 
I've been looking for the most stl-ish way to remove elements that 
fulfill a certain condition from a vector. I thought this would work:

struct SmallerThan5
{
   bool operator()(int i)
   {
     return i < 5;
   }
}
std::vector vec;
// fill vec with a bunch of value
vec.erase(std::find(vec.begin(), vec.end(), SmallerThan5()));

... but this only removes the first value, because the version of erase 
that takes 1 iterator only removes the element the iterator points to. 
Is there a one-line way? Oh and how would I in-line the SmallerThan5 
class with boost::lambda or boost::bind? Is it possible at all? Thanks.


cheers,

roel



Ehsan Akhgari wrote:

>  > Now, at anyone time, from 2 to 5 of the items will be populated
>  > (starting index 0).  And I need to shuffle just those entries up like
>  > this:
>  >
>  > Imagine that indexes 0, 1 and 2 are used.  Then 1 moves to 0,
>  > 2 to 1, and 0 to 2.
> 
> Aha, now I see.  So you don't want to "shuffle" them, you want to rotate
> them, right?  This is what I would do:
> 
> #include <algorithm>
> 
> void f()
> {
>         int array[ 5 ];
>         // populate it
> 
>         // rotate the first three items
>         std::rotate( array, array + 1, array + 3 ); }
> 
> Pretty straightforward.
> 
> -------------
> Ehsan Akhgari
> 
> Farda Technology (http://www.farda-tech.com/)
> 
> List Owner: [EMAIL PROTECTED]
> 
> [ Email: [EMAIL PROTECTED] ]
> [ WWW: http://www.beginthread.com/Ehsan ]
> 
> But the thought is one thing, the deed another, and the idea of the deed
> still another. The wheel of causality does not roll between them.
> -Thus Spoke Zarathustra, F. W. Nietzsche
> 
> 
> 
> 
> _______________________________________________
> msvc mailing list
> [EMAIL PROTECTED]
> See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for 
> subscription changes, and list archive.
> 


_______________________________________________
msvc mailing list
[EMAIL PROTECTED]
See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for subscription 
changes, and list archive.

Reply via email to