Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-03-11 08:38:23


From: "rwgk" <rwgk_at_[hidden]>
> --- In boost_at_y..., "Peter Dimov" <pdimov_at_m...> wrote:
> > Passing the function to the vector constructor solves the
> > problem - and leaves the vector initialized so that its
> > other methods work after construction, as is the case now.
>
> That sounds ideal (and I believe is very similar to what
> David is advocating), but I am having difficulties to see
> how it can be implemented in an easy-to-use way (and in a
> way that does not interfere with the existing interface,
> using a mechanism that can be understood and implemented by
> programmers with less than 5+ years of full-time C++
> development experience).
> Here is a moderately difficult challenge ("use push_back()" does
> not count as an answer):
>
> void foo(std::vector<double> const& a1, std::vector<double> const& a2)
> {
> std::vector<double> r(a1.size(), uninitialized_flag());
> for(std::size_t i=0;i<r.size();i++) {
> r[i] = std::pow(a1[i], a2[i]);
> }
> }
>
> How would you do that?

void init(double * r, double const * a1, double const * a2, size_t n)
{
    for(size_t i = 0; i < n; ++i) r[i] = std::pow(a1[i], a2[i]);
}

std::vector<double> r(a1.size(), bind(init, _1 /* &r[0] */, &a1[0], &a2[0],
_2 /* r.size() */));

From the 'bind as signature adaptor' dept. ;-)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk