Created
November 24, 2012 13:22
-
-
Save DCoderLT/4139671 to your computer and use it in GitHub Desktop.
Possible solution for SO question #13541243
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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