首页 > 解决方案 > 使用 std::accumulate(v.begin(), v.end(), 0) 发出警告;

问题描述

为什么我会收到警告C4244 '=': conversion from '__int64' to '_Ty', possible loss of data?此外,auto而不是LONGLONG产生此警告。

std::vector<LONGLONG>& v = m_vecEstimatedTime;
LONGLONG nEstimatedTimeTotal = std::accumulate(v.begin(), v.end(), 0);  //  warning C4244  '=': conversion from '__int64' to '_Ty'

标签: c++

解决方案


返回类型和汇总值的类型std::accumulate由传递的初始值决定。因为0它会int

如果类型应该是LONGLONG,您可以将初始值指定为:

LONGLONG nEstimatedTimeTotal = std::accumulate(v.begin(), v.end(), static_cast<LONGLONG>(0));

推荐阅读