求向量间的点积

#include <iostream>
#include <vector>
#include <numeric>

int main() {
  double a[]={1, 3, 5.4};
  double b[]={1, 5, 7};

  std::vector<double> v_a, v_b;

  for(size_t i=0;i<sizeof(a)/sizeof(a[0]);++i) {
    v_a.push_back(a[i]);
    v_b.push_back(b[i]);
  }

  std::cout<<"inner product: "<<inner_product(v_a.begin(), v_a.end(), v_b.begin(), 0.0)<<std::endl;

  return 0;
}

inner_product()算法定义在名字空间std里,由<numeric>给出:

template <class In, class In2, class T>
T inner_product(In first, In last, In2 first2, T init) {
  while(first != last)
    init=init + *first++ * *first2++;

  return init;
}

posted @ 2018-08-25 15:01  东宫得臣  阅读(176)  评论(0编辑  收藏  举报