std::accumulate

std::accumulate#

std::accumulate() 是 C++ 标准模板库(STL)中的函数,用于返回一系列值的总和。它接受三个参数:

 std::vector<int> numbers = {1, 2, 3, 4, 5};

    int sum = std::accumulate(numbers.begin(), numbers.end(), 0); // sum starts at zero
std::vector<int> numbers = {1, 2, 3, 4, 5};

std::accumulate(numbers.begin(), numbers.end(), 0)
15