132得票25回答
如何在列表中找到数字的累计和?

time_interval = [4, 6, 12] 我想将数字相加,例如[4, 4+6, 4+6+12],以获得列表t = [4, 10, 22]。 我尝试了以下方法: t1 = time_interval[0] t2 = time_interval[1] + t1 t3 = tim...

49得票5回答
理解std::accumulate

我想知道为什么std::accumulate(又称reduce)需要第三个参数。对于不知道accumulate是什么的人来说,它像这样使用:vector<int> V{1,2,3}; int sum = accumulate(V.begin(), V.end(), 0); //...

27得票6回答
如何使用std :: accumulate和lambda计算平均值?

我有一个标准库的容器存储大数值,它们非常大以至于如果将它们相加可能会导致溢出。假设这个容器是这样的:std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 我想用std::accumulate计算这个容器的平均值,但我不能把所有数...

26得票1回答
为什么C++标准中没有类似的`std::accumulate`函数的并行版本?

我认为C++标准中没有std::accumulate的并行版本是令人困惑的。在我看来,基于OpenMP或SIMD指令实现它并行化应该是很简单的。有人能解释一下为什么标准委员会选择引入std::reduce的并行版本而不是std::accumulate吗?是因为迭代器类型不同吗?

22得票2回答
能否使用std::accumulate和std::min一起使用?

我正在尝试将 std::accumulate 与 std::min 结合使用。类似这样(无法编译):vector<int> V{2,1,3}; cout << accumulate(V.begin()+1, V.end(), V.front(), std::min...

20得票5回答
使用std::accumulate函数将map的所有值相加

我只是想在下面程序中添加一个定义的映射值: std::map<int, int> floor_plan; const size_t distance = std::accumulate(std::begin(floor_plan), std::end(floor_plan), ...

20得票2回答
为什么在C++20中std::accumulate没有被设置为constexpr?

在C++20中,许多(大多数?)C++标准库算法已被制作为constexpr。然而-std::accumulate并没有。 看起来它本可以是:链接 template<class InputIt, class T> constexpr T accumulate(InputIt f...

19得票4回答
为什么std::accumulate函数显示vector<double>的错误总和?

考虑下面用于将 vector 中所有元素相加的代码:#include&lt;iostream&gt; #include&lt;algorithm&gt; #include&lt;numeric&gt; #include&lt;vector&gt; using namespace std; in...

13得票1回答
使用累加求包含长整型的向量的总和

#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;numeric&gt; #include &lt;iterator&gt; using namespace std; int main() { int N;...

12得票1回答
如何在std::reduce和std::accumulate之间做出选择?

std::accumulate 和 std::reduce 基本上做的是一样的事情。 std::reduce 的摘要已经说得很清楚了: similar to `std::accumulate`, except out of order 在许多情况下,这些函数应该产生相同的结果并展现相...