Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Possible solution for SO question #13541243
#include <vector>
#include <numeric>
#include <functional>
template<typename T>
std::vector<T> compact(const std::vector<T> &input, int stride, std::function<T(T, T)> op) {
std::vector<T> output(stride);
unsigned int index = 0;
return std::accumulate(input.begin(), input.end(), output, [&index, stride, op](std::vector<T> &acc, T elem) -> std::vector<T> {
acc[index % stride] = op(acc[index % stride], elem);
++index;
return acc;
});
}
int main(int argc, char* argv[])
{
std::vector<int> input;
for(unsigned int i = 1; i < 11; ++i) {
input.push_back(i);
}
auto output = compact(input, 3, std::function<int(int, int)>([](int l, int r) {
return l + r;
}));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.