from<numeric>

https://en.cppreference.com/w/cpp/algorithm/partial_sum

计算前缀和很方便的函数

code:

    std::vector<long long > v(10, 2); // v = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}

    std::cout << "The first " << v.size() << " even numbers are: ";
    // write the result to the cout stream
    std::partial_sum(v.cbegin(), v.cend(),
                     std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';

    // write the result back to the vector v
    std::partial_sum(v.cbegin(), v.cend(),
                     v.begin(), std::multiplies<int>());

    std::cout << "The first " << v.size() << " powers of 2 are: ";
    for (long long  n : v)
        std::cout << n << ' ';
    std::cout << '\n';
    //use lambda
    std::partial_sum(v.cbegin(), v.cend(),
                     v.begin(), [](const auto&a,const auto &b){
                return a*b;
    });
    std::cout << "The first " << v.size() << " powers of 2 are: ";
    for (long long  n : v)
        std::cout << n << ' ';
    std::cout << '\n';

output:

The first 10 even numbers are: 2 4 6 8 10 12 14 16 18 20
The first 10 powers of 2 are: 2 4 8 16 32 64 128 256 512 1024
The first 10 powers of 2 are: 2 8 64 1024 32768 2097152 268435456 68719476736 35184372088832 36028797018963968