[英]int32_t and int64_t Conversion Issues
In the following line of code, positive
is a vector of int32_t
elements.在下面的代码行中,
positive
是一个包含int32_t
元素的向量。
int32_t pos_accum = accumulate(positive.begin(), positive.end(), std::multiplies<int32_t>());
The compiler generates the following error that seems to make no sense:编译器生成以下似乎没有意义的错误:
No suitable conversion function from "std::multiplies<int32_t>" to "int32_t" exists
as well as也
'initializing': cannot convert from '_Ty' to 'int32_t'
The ultimate goal is to store the answer in an int64_t
, but attempts to static_cast<int64_t>
the result ends up in similar errors.最终目标是将答案存储在
int64_t
中,但尝试static_cast<int64_t>
结果以类似错误告终。
Any idea what the problem is here?知道这里的问题是什么吗?
The signature of std::accumulate
that accepts a binary operator is接受二元运算符的
std::accumulate
的签名是
template< class InputIt, class T, class BinaryOperation >
constexpr T accumulate( InputIt first, InputIt last, T init, BinaryOperation op );
You're missing the init
argument, which represents the initial value of the accumulation.您缺少
init
参数,它表示累加的初始值。 Try尝试
accumulate(positive.begin(), positive.end(), 1, std::multiplies<int32_t>());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.