Boost logo

Boost Users :

Subject: Re: [Boost-users] [phoenix] another problem with member_variable bind
From: Adam Merz (adammerz_at_[hidden])
Date: 2008-11-07 18:15:14


If std::for_each is acceptable then the following works. Or do you specifically
need std::transform?

//////////////////////

#include <algorithm>
#include <vector>
#include <iostream>

#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>

struct point
{
    double _prob;
    double _x;
};

point make_point(double prob, double x)
{
    point const p = { prob, x };
    return p;
}

int main()
{
    namespace phx = boost::phoenix;
    using namespace phx::arg_names;

    typedef std::vector<point> points_t;

    points_t points;
    points.push_back(make_point(1., 5.));
    points.push_back(make_point(2., 6.));
    points.push_back(make_point(3., 7.));
    points.push_back(make_point(4., 8.));

    std::for_each(
        points.begin(),
        points.end(),
        phx::bind(&point::_prob, _1) *= 2.
    );

    for (points_t::const_iterator i = points.begin(), i_end = points.end();
            i != i_end; ++i)
        std::cout << i->_prob << ' ' << i->_x << std::endl;

    return 0;
}


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