gpt4 book ai didi

c++ - map 的内积

转载 作者:行者123 更新时间:2023-11-28 01:04:06 25 4
gpt4 key购买 nike

inner_product 不能应用于 map 吗?我有以下代码:

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 错误不匹配的函数。

最佳答案

它当然可以应用于 map ,给定创造性地选择仿函数,但 inner_product 需要迭代器,而 vit->first 是一个 const Avit->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;});

关于c++ - map 的内积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7278966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com