Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Moving average using variadic templates. Takes average of consecutive 3-number sequence.
#include <iostream>
using namespace std;
template <typename U1, typename U2, typename U3>
void movingAvg(U1 u1, U2 u2, U3 u3) {
cout << "AVG(" << u1 << ", " << u2 << ", " << u3 << ") = " << (u1 + u2 + u3) / 3.f << endl;
}
template <typename U1, typename U2, typename U3, typename... Ts>
void movingAvg(U1 u1, U2 u2, U3 u3, Ts... ts) {
movingAvg(u1, u2, u3);
movingAvg(u2, u3, ts...);
}
int main() {
cout << "Example 2: moving average" << endl;
movingAvg(4, 16, 78, 0, 15, -300, 680);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.