How to do the calculation with std::transform

时间:2016-06-10 16:13:46

标签: c++ c++11

I have a void function that takes as arguments iterators to the start and the end of a set of points (Setiterator set_begin, Setiterator set_end),a query point to perform some calculations and a iterator to the beginnig of a set of points ( where I am going to add the results)

void computedist(Setiterator set_begin, Setiterator set_end,
Vector const& query_point, DistanceIterator dist_begin )
{
    std::transform(set_begin, set_end, dist_begin, calculation);

}

I have read that with std::transform I can do that calculations over the whole set of points, but I don't know how should I define the calculation to be done, as I am new to C++.

In this case, I want to compute the distance of the points to the query point:

I guess that my calculation should look like this

double calc_dist(double query_point, double point_of_the_set){
     double dist;
     dist = fabs(query_point - point_of_the_set);
     return dist;

But I don't know how should I give the arguments to the function, since I am new to working with iterators.

Thank's!

1 个答案:

答案 0 :(得分:1)

你正在调用的std::transform的味道需要一个UnaryOp函数来调用它所传递的函数' * iterator'通过const引用,并期望它写入iterator::value_type的{​​{1}}的返回值。

*dest

现场演示:http://ideone.com/CUUlvA