为C ++实践构建统计程序,我从文件中提取主题的年龄并将其存储在deque<int> ages
中。但是,当我尝试获得标准偏差时,我遇到了错误:
template <class U> typename U::value_type mean(U begin,U end){
typedef typename U::value_type res_type;
res_type sum=res_type();
size_t count=0;
for(U pos=begin;pos!=end;++pos){
sum+=*pos;
++count;
}
return sum/count;
}
template <typename T>
double stan_dev(deque<T>&d){
double dev_sum=0,dev,dev_sqr,st_dev;
double mn=mean(d.begin(),d.end());
for(int it=0;it<d.size();++it){
T val=d[it];
dev= val-mn;
dev_sqr = dev*dev;
dev_sum+=dev_sqr;
cout<<"\n Deviation at for age # "<<d[it]<<" is "<<dev;
cout<<"\n Deviation squared for age # " <<d[it]<<" is "<<dev_sqr;
cout<<"\n Summing up so far .. "<<dev_sum;
}
st_dev=sqrt((dev_sum/d.size()));
return st_dev;
}
只需在此处提供相关部分cout << stan_dev(ages)
,程序就会挂起。请帮忙。
答案 0 :(得分:0)