fork download
  1. #include <functional>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. template <typename LambdaT, typename R, typename... Args>
  6. auto _LambdaToStdFunction(LambdaT lambda, R (LambdaT::*)(Args...) const) {
  7. return std::function<R(Args...)>(lambda);
  8. }
  9. template <typename LambdaT>
  10. auto LambdaToStdFunction(LambdaT &&lambda) {
  11. return _LambdaToStdFunction(std::forward<LambdaT>(lambda),
  12. &LambdaT::operator());
  13. }
  14.  
  15. template <typename A,typename B>
  16. std::vector<B> map(std::function<B (A)> f, std::vector<A> arr) {
  17. std::vector<B> res;
  18. for (int i=0;i<arr.size();i++) res.push_back(f(arr[i]));
  19. return res;
  20. }
  21.  
  22.  
  23. int main() {
  24. std::vector<int> a = {1, 2, 3};
  25. auto f = LambdaToStdFunction([](int x) -> int {
  26. std::cout << x << std::endl;
  27. return x;
  28. });
  29. map(LambdaToStdFunction([](int x) -> int {
  30. std::cout << x << std::endl;
  31. return x;}),a); //now OK
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 4784KB
stdin
Standard input is empty
stdout
1
2
3