Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <functional>
- #include <string>
- using namespace std;
- template<typename T>
- function<T(T)> rep(function<T(T)> f, int n) {
- return [f,n](T x) -> T {
- T res = x;
- for(int i=1; i<=n; ++i) {
- res = f(res);
- }
- return res;
- };
- }
- int square(int x) {
- return x*x;
- }
- string app(string x) {
- return x+"!";
- }
- int main() {
- cout << rep<int>(square, 1)(3) << endl;
- cout << rep<int>(square, 2)(3) << endl;
- cout << rep<int>(square, 3)(3) << endl;
- cout << rep<string>(app, 3)("Hello World") << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement