C++ c+中的加权加法+;
通过以下代码(使用c++14编译),可以逐个元素添加两个数组并将结果保存到第一个数组中: 如果我想得到以下信息怎么办?std::transform或std算法中的任何其他功能是否仍然可以实现这一点C++ c+中的加权加法+;,c++,C++,通过以下代码(使用c++14编译),可以逐个元素添加两个数组并将结果保存到第一个数组中: 如果我想得到以下信息怎么办?std::transform或std算法中的任何其他功能是否仍然可以实现这一点 a[i] = a[i] * weight + b[i] * (1. - weight) 非常感谢…传统的方法是有状态函子。现在,我们可以用一辆lambda来做: #include <algorithm> #include <iostream> #include <arr
a[i] = a[i] * weight + b[i] * (1. - weight)
非常感谢…传统的方法是有状态函子。现在,我们可以用一辆lambda来做:
#include <algorithm>
#include <iostream>
#include <array>
int main()
{
std::array<double,3> a = {1, 2, 3};
std::array<double,3> b = {4, 5, 6};
const double weight = 0.7;
std::transform(a.begin( ), a.end( ), b.begin( ), a.begin( ),
[weight](double aa, double bb) {
return aa*weight + bb*(1. - weight);
}
);
for(double d : a)
std::cout << d << std::endl;
return 0;
}
#包括
#包括
#包括
int main()
{
数组a={1,2,3};
数组b={4,5,6};
常数双倍重量=0.7;
std::transform(a.begin(),a.end(),b.begin(),a.begin(),
[重量](双aa,双bb){
返回aa*重量+bb*(1.-重量);
}
);
对于(双d:a)
std::cout非lambda解决方案(与C++03兼容)应为:
#include <algorithm>
#include <iostream>
#include <array>
struct MyFunctor
{
const double _weight;
MyFunctor(double weight) : _weight(weight) {}
double operator()(double a, double b) const
{
return a * _weight + b * (1. - _weight);
}
};
int main()
{
std::array<double,3> a = {1, 2, 3};
std::array<double,3> b = {4, 5, 6};
double weight = 0.7;
std::transform(a.begin( ), a.end( ), b.begin( ), a.begin( ), MyFunctor(weight));
for(double d : a)
std::cout << d << std::endl;
return 0;
}
#包括
#包括
#包括
结构MyFunctor
{
常数双倍重量;
MyFunctor(双重权重):_权重(权重){}
双运算符()(双a,双b)常量
{
返回a*_-weight+b*(1.-_-weight);
}
};
int main()
{
数组a={1,2,3};
数组b={4,5,6};
双倍重量=0.7;
std::transform(a.begin()、a.end()、b.begin()、a.begin()、MyFunctor(weight));
对于(双d:a)
std::不能使用lambda并在lambda正文中定义功能。请给出“有状态函子”的示例。这将允许对技术进行比较。(IMO,lambda很难阅读。)函数的麻烦在于,它们涉及到函数外定义类——这意味着将代码行与其使用分离。@吉尔巴茨:太晚了。Functor是C++中的接受术语。有点令人伤心,但它是如何。麻烦。你的答案和我的编辑在以太交叉。@ MartinBonner:
#include <algorithm>
#include <iostream>
#include <array>
int main()
{
std::array<double,3> a = {1, 2, 3};
std::array<double,3> b = {4, 5, 6};
const double weight = 0.7;
std::transform(a.begin( ), a.end( ), b.begin( ), a.begin( ),
[weight](double aa, double bb) {
return aa*weight + bb*(1. - weight);
}
);
for(double d : a)
std::cout << d << std::endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <array>
struct weighted_add {
const double weight;
double operator() const (double aa, double bb) {
return aa*weight + bb*(1.-weight);
}
weighted_add(double weight_) : weight(weight_) {}
};
int main()
{
std::array<double,3> a = {1, 2, 3};
std::array<double,3> b = {4, 5, 6};
std::transform(a.begin( ), a.end( ), b.begin( ), a.begin( ), weighted_add(0.7));
for(double d : a)
std::cout << d << std::endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <array>
struct MyFunctor
{
const double _weight;
MyFunctor(double weight) : _weight(weight) {}
double operator()(double a, double b) const
{
return a * _weight + b * (1. - _weight);
}
};
int main()
{
std::array<double,3> a = {1, 2, 3};
std::array<double,3> b = {4, 5, 6};
double weight = 0.7;
std::transform(a.begin( ), a.end( ), b.begin( ), a.begin( ), MyFunctor(weight));
for(double d : a)
std::cout << d << std::endl;
return 0;
}