nietsnegttiw
nietsnegttiw

Reputation: 371

std::transform Vector For Euclidean Distance

I would like to find the optimal approach to computing the Euclidean distance of some points in a vector. I am trying to use a std::transform to square each number, then pass this function as an argument into std::accumulate. Is this the most efficient way? As of right now my implementation has a bug, the error is:

in instantiation of function template specialization 'std::__1::accumulate<std::__1::__wrap_iter<int *>, int, std::__1::__wrap_iter<int *> >' requested
      here
  cout << std::sqrt(std::accumulate(v.begin(), v.end(), 0, std::transform(v.begin(), v.end(), v.begin(), square))) << endl;

This is my code:

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>

using std::cout;
using std::endl;
using std::string;

int square(int n) {return n*n;}

int
main()
{
  std::vector<int> v;
  std::vector<int> v1;

  for(int i = 0; i < 10; ++i)
    {v.push_back(i);}
  v1.resize(v.size());
  //std::transform (v.begin(), v.end(), v1.begin(), square);                                                                                                                         
  //std::copy(v.begin(), v1.begin(), std::ostream_iterator<int>(cout, ", "));                                                                                                        


  //cout << std::sqrt(std::accumulate(v1.begin(), v1.end(), 0)) << endl;                                                                                                             
  cout << std::sqrt(std::accumulate(v.begin(), v.end(), 0, std::transform(v.begin(), v.end(), v.begin(), square))) << endl;

  //for(auto it = v1.begin(); it != v1.end(); it++)                                                                                                                                  
    //{cout << *it << endl;}                                                                                                                                                         

  return 0;
}

Upvotes: 1

Views: 3330

Answers (2)

bames53
bames53

Reputation: 88155

This is exactly what std::inner_product is for.

#include<iostream> // cout
#include<vector>   // vector
#include<numeric>  // inner_product, iota
#include<cmath>    // sqrt

using std::cout;

int
main()
{
  std::vector<int> v(10);
  std::iota(begin(v), end(v), 0);

  double result = std::sqrt(std::inner_product(begin(v), end(v), begin(v), 0));

  cout << result << '\n';    
}

Upvotes: 5

vsoftco
vsoftco

Reputation: 56557

You need to first transform then accumulate the transformed vector

std::transform(v.begin(), v.end(), v.begin(), square);                                                                                                       
cout << std::sqrt(std::accumulate(v.begin(), v.end(), 0)) << endl;

as std::transform does not (yet) return a range. If/when Eric Niebler's ranges will make it to C++17, you'll be able to write this code much more concisely and elegantly.

Live on Coliru

In terms of efficiency, I'd say it's quite efficient. Don't worry about micro-optimization unless you know (from a profiler) that you hit a "hot" area of your code.

Upvotes: 6

Related Questions