inner_product of map

inner_product of map

本文关键字:map of product inner      更新时间:2023-10-16

inner_product可以不应用于映射吗?我有以下代码:

std::map<class A, class B> s;
std::map<class A, class B>::const_iterator vit=s.begin();
long double x = std::inner_product(vit->first,vit->second,vit->first,0.0);

但我得到一个不匹配的函数inner_product错误

它当然可以应用于映射,给定创造性地选择的函子,但inner_product期望迭代器,而vit->firstconst A, vit->secondB

例如,

std::map<int, double> m = {{1, 0.1}, {2, 0.2}};
typedef std::map<int, double>::value_type val_t;
double x = std::inner_product(m.begin(), m.end(), m.begin(), 0.0,
           std::plus<double>(),
           [](val_t lhs, val_t rhs){return lhs.first * rhs.second;});