for_each()算法 [英] for_each() algorithm

查看:84
本文介绍了for_each()算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个计算标准差的函数。我想用
用STL算法重写它。

我正在考虑使用for_each()算法,但这需要我的

函数传入有状态(例如标准差的值

sum)。我读了一些在线文章,说状态不是很好的想法。那么有更好的解决方案吗?


谢谢。


/ **

计算标准偏差,给出数字的平均值

* /

double sd(const vector< int>& v,

const double mean)

{

double stdDev = 0.0;

int N = v.size();

const double mean_ = mean ;


//计算标准差金额

double stdDevSum = 0;

double x;

for(int i = 0; i< N; i ++){

x = v [i] - mean_;

stdDevSum = stdDevSum +(x * x) ;

}

double variance = stdDevSum / static_cast< double>(N-1);

stdDev = sqrt(variance);

返回stdDev;

} // sd


I have a function which calculate standard deviation. I am trying to
re-write it using STL algorithm.
I am thinking of using for_each() algorithm, but that will require my
function passing in to have state (e.g. the value of standard deviation
sum). I read some online article that having state is not a good
idea. So is there a better solution?

Thank you.

/**
Calculate the standard deviation, given the mean of the numbers
*/
double sd( const vector<int>& v,
const double mean )
{
double stdDev = 0.0;
int N = v.size();
const double mean_ = mean;

// calculate the standard deviation sum
double stdDevSum = 0;
double x;
for (int i = 0; i < N; i++) {
x = v[i] - mean_;
stdDevSum = stdDevSum + (x * x);
}
double variance = stdDevSum / static_cast<double>(N-1);
stdDev = sqrt( variance );
return stdDev;
} // sd

推荐答案

si *************** @ gmail.com 写道:
我有一个计算标准差的函数。我正在尝试使用STL算法重新编写它。
我正在考虑使用for_each()算法,但这需要我的
函数传入以获得状态(例如,标准差
总和)。我读了一些在线文章,说状态不是一个好主意。那么有更好的解决方案吗?
I have a function which calculate standard deviation. I am trying to
re-write it using STL algorithm.
I am thinking of using for_each() algorithm, but that will require my
function passing in to have state (e.g. the value of standard deviation
sum). I read some online article that having state is not a good
idea. So is there a better solution?




std :: for_each()是一般规则的例外:函数对象

与之一起使用算法可能有状态,这就是

算法返回函数对象的原因。

祝你好运,


Tom



std::for_each() is an exception to the general rule: Function objects
used with that algorithm may have state, and that is the reason that
algorithm returns the function object.

Best regards,

Tom



si *************** @ gmail.com 写道:
我有一个计算标准差的函数。我正在尝试使用STL算法重新编写它。
我正在考虑使用for_each()算法,但这需要我的
函数传入以获得状态(例如,标准差
总和)。我读了一些在线文章,说状态不是一个好主意。那么有更好的解决方案吗?
I have a function which calculate standard deviation. I am trying to
re-write it using STL algorithm.
I am thinking of using for_each() algorithm, but that will require my
function passing in to have state (e.g. the value of standard deviation
sum). I read some online article that having state is not a good
idea. So is there a better solution?




函数对象。使用operator()声明一个类StdDeviation,以便

这个类的对象可以作为第三个参数传递给for_each。

stdDevSum将是这样一个类的数据成员,通过no-arg构造函数初始化为零





Function Objects. Declare a class StdDeviation with operator() so that
an object of this class can be passed as third parameter to for_each.
stdDevSum will be a data member of such a class, initialized to zero
via no-arg constructor.



si *************** @ gmail.com 写道:
我有一个计算标准差的函数。我正在尝试使用STL算法重新编写它。
我正在考虑使用for_each()算法,但这需要我的
函数传入以获得状态(例如,标准差
总和)。我读了一些在线文章,说状态不是一个好主意。那么有更好的解决方案吗?
I have a function which calculate standard deviation. I am trying to
re-write it using STL algorithm.
I am thinking of using for_each() algorithm, but that will require my
function passing in to have state (e.g. the value of standard deviation
sum). I read some online article that having state is not a good
idea. So is there a better solution?




是的。采用你拥有的算法,并用适当的算法替换for循环和

。在这种情况下,你总结了

(x [i] -xavg)*(x [i] -xavg)。

现在,总结一些值最好由std :: accumulate完成。


HTH,

Michiel Salters



Yes. Take the algorithm you have, and replace just the for-loop with
the appropriate algorithm. In this case, you''re summing
(x[i]-xavg)*(x[i]-xavg).
Now, summing a number of values is best done by std::accumulate.

HTH,
Michiel Salters


这篇关于for_each()算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆