开发者

inner_product of map

can inner_product not be applied to a map. I have the following code:

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);

but I 开发者_Python百科get a no-matching function to inner_product error.


It certainly can be applied to maps, given creatively chosen functors, but inner_product expects iterators, while vit->first is a const A and vit->second is a B.

For example,

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;});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜