扩展代码,

#include <future>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

double accum(double* beg, double* end, double init) { return accumulate(beg, end, init); }

double comp4(vector<double>& v) {
    if (v.size() < 10000) {
        double* first = &v[0];
        return accum(first, first + v.size(), 0.0);
    }

    auto v0 = &v[0];
    auto sz = v.size();

    auto f0 = async(accum, v0, v0 + sz / 4, 0.0);               // first quarter
    auto f1 = async(accum, v0 + sz / 4, v0 + sz / 2, 0.0);      // second quarter
    auto f2 = async(accum, v0 + sz / 2, v0 + sz * 3 / 4, 0.0);  // third quarter
    auto f3 = async(accum, v0 + sz * 3 / 4, v0 + sz, 0.0);      // fourth quarter

    return f0.get() + f1.get() + f2.get() + f3.get();  // collect and combine the results
}

int main() {
    vector<double> v{1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5};

    double result = comp4(v);

    cout << "Result: " << result << endl;

    return 0;
}