Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-02-02 15:44:04


Meryl Silverburgh wrote:
> Hi,
>
> I have a sorted int vector 'orgWidth'. I would like to save the save
> into a new int vector 'newWidth' if the value of the current element
> is 10% bigger than the previous one.
>
> For example,
> orgWidth int vector : 300 366 729 769 781 1297
> newWidth int vector: 300 366 729 1297
>
> I know how to add the value to the newWidth vector using the if_then
> with boost. But my question is how can I update the value of the
> 'previousWidth' as I use for_each() algorithm.

I'm not sure if it's useful to you, but the boost::bind solution is to use a
helper function:

#include <boost/bind.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>

static bool above_ten_percent( int & last, int x )
{
    bool r = x > last * 1.1;
    last = x;
    return r;
}

int main()
{
    int v[] = { 300, 366, 729, 769, 781, 1297 };

    std::remove_copy_if( v, v + 6,
        std::ostream_iterator<int>( std::cout, " " ),
        !boost::bind( above_ten_percent, 0, _1 ) );
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net