Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. template<typename T>
  8. function<T(T)> rep(function<T(T)> f, int n) {
  9.     return [f,n](T x) -> T {
  10.         T res = x;
  11.         for(int i=1; i<=n; ++i) {
  12.             res = f(res);
  13.         }
  14.         return res;
  15.     };
  16. }
  17.  
  18. int square(int x) {
  19.     return x*x;
  20. }
  21.  
  22. string app(string x) {
  23.     return x+"!";
  24. }
  25.  
  26. int main() {
  27.     cout << rep<int>(square, 1)(3) << endl;
  28.     cout << rep<int>(square, 2)(3) << endl;
  29.     cout << rep<int>(square, 3)(3) << endl;
  30.     cout << rep<string>(app, 3)("Hello World") << endl;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement