使用 std::accumulate
需要以下示例的更漂亮的解决方案,但使用 std::accumulate。
#include <algorithm>
#include <vector>
#include <iostream>
class Object
{
public:
Object( double a, double b ):
a_( a ),
b_( b )
{}
double GetA() const { return a_; }
double GetB() const { return b_; }
// other methods
private:
double a_;
double b_;
};
class Calculator
{
public:
Calculator( double& result ):
result_( result )
{}
void operator() ( const Object& object )
{
// some formula
result_ += object.GetA() * object.GetB();
}
private:
double& result_;
};
int main()
{
std::vector< Object > collection;
collection.push_back( Object( 1, 2 ) );
collection.push_back( Object( 3, 4 ) );
double result = 0.0;
std::for_each( collection.begin(), collection.end(),
Calculator( result ) );
std::cout << "result = " << result << std::endl;
return 0;
}
Need prettier solution of below example but with std::accumulate.
#include <algorithm>
#include <vector>
#include <iostream>
class Object
{
public:
Object( double a, double b ):
a_( a ),
b_( b )
{}
double GetA() const { return a_; }
double GetB() const { return b_; }
// other methods
private:
double a_;
double b_;
};
class Calculator
{
public:
Calculator( double& result ):
result_( result )
{}
void operator() ( const Object& object )
{
// some formula
result_ += object.GetA() * object.GetB();
}
private:
double& result_;
};
int main()
{
std::vector< Object > collection;
collection.push_back( Object( 1, 2 ) );
collection.push_back( Object( 3, 4 ) );
double result = 0.0;
std::for_each( collection.begin(), collection.end(),
Calculator( result ) );
std::cout << "result = " << result << std::endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更改计算器和主要功能。
它还可以更好:
do changes in Calculator and main function.
also it could be better:
更新 2: Boost.Lambda 使这变得轻而易举:
更新: 这已经困扰我一段时间了。 我无法让任何 STL 算法以良好的方式工作。 所以,我自己推出了:
当然,原来的解决方案仍然有效:
最简单的方法是实现一个
operator+
。 在这种情况下:并使其成为
Object
或成员的友元(查找为什么您可能更喜欢其中一个而不是另一个):然后您就完成了:
我之前的方法不起作用,因为我们需要一个
binary_function
。std::accumulate 手册。
Update 2: Boost.Lambda makes this a piece of cake:
Update: This has been bugging me for a while. I can't just get any of the STL algorithms to work in a decent manner. So, I rolled my own:
Of course, the original solution still holds:
The easiest is to implement an
operator+
. In this case:and make it a friend of
Object
or member (look up why you may prefer one over the other):and you're done with:
My earlier approach doesn't work because we need a
binary_function
.std::accumulate manual.
这里有一个问题,我猜参数的编写顺序应该是错误的:
在这种累加的情况下,结果包含在返回的对象中。
如果您使用 gnu 并行模式,如果结果与迭代器引用的实际对象不同,函子会给您带来问题。
由于一些奇怪和愚蠢的原因,不能在 gnu 并行模式下工作。
here is an issue here, I guess the arguments are written in the wrong order should be:
in this case of accumulate, the result is contained in a returned Object.
If you are using gnu parallel mode the functor will give you problems if the result and the actual object referred to by the iterator are different.
will not work with gnu parallel mode for some strange and silly reason.
使用c++0x:
Using c++0x:
人们希望这是一份家庭作业……
并
假设您不被允许接触 Object 的声明。
One would hope this is homework...
and
assuming you're not allowed to touch the declaration of Object.