我试图对向量使用累积函数
vector <double> A;
double B = 0;
A.reserve(100);
for(itr = 0; itr < 210; itr++)
{
term1 = pow(r[itr], 12);
term1 = 1/term1;
term2 = pow(r[itr], 6);
term2 = 2/term2;
A.push_back(term1 - term2);
}
B = accumulate(A.begin(), A.end(), 0);
但是,我总是得到 B = 0,而 A 具有非零值
std::accumulate
有点狡猾,因为结果的类型是初始值的类型,并且not容器元素的类型!所以你的累加器产生int
s.
要解决此问题,请累积到double
:
accumulate(A.begin(), A.end(), 0.0);
// ^^^^^^^ literal of type double
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)