Last active
February 27, 2017 17:21
-
-
Save kantoniak/f53c4e1d83413cc949100ed2a09193cd to your computer and use it in GitHub Desktop.
Moving average using variadic templates. Takes average of consecutive 3-number sequence.
This file contains 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 <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