1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <vector>
#include <numeric>
#include <iostream>
template <typename T>
double invsum( double lhs, T rhs ) { return lhs + 1.0 / rhs; }
int main() {
std::vector<std::vector<int>> foo { {15, 41}, {1, 2, 3, 4}, {22, 11, 24} };
std::vector<double> sums( foo.size() );
for ( size_t dataset = 0; dataset < foo.size(); ++dataset ) {
const auto & bar = foo[dataset];
sums[dataset] = std::accumulate( std::begin(bar), std::end(bar), 0.0, invsum<int> );
}
for ( auto sum : sums ) {
std::cout << sum << '\n';
}
std::cout << "Total: "
<< std::accumulate( std::begin(sums), std::end(sums), 0.0, invsum<double> )
<< '\n';
return 0;
}
|