std::accumulate folds a range into one value, such as a total with an optional starting amount.

Accumulate Totals

accumulate_totals.cpp
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> prices{12, 8, 5};
    int shipping = ;

    int total = std::accumulate(prices.begin(), prices.end(), shipping);

    std::cout << "shipping=" << shipping << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> prices{12, 8, 5};
    int shipping = ;

    int total = std::accumulate(prices.begin(), prices.end(), shipping);

    std::cout << "shipping=" << shipping << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> prices{12, 8, 5};
    int shipping = ;

    int total = std::accumulate(prices.begin(), prices.end(), shipping);

    std::cout << "shipping=" << shipping << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
accumulate `std::accumulate` visits each value in a range and combines it into a running result.
initial value The initial value controls where the total starts before the first element is added.