fork download
  1. #include <functional>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. template <class RetType, class Arg1, class ...Args>
  6. std::vector<RetType> map(RetType (*fnc)(Arg1, Args...), std::vector<Arg1> arg1, std::vector<Args> ... args)
  7. {
  8. // TODO assert что все вектора имеют одинаковый размер
  9. std::vector<RetType> ret;
  10. for(int i = 0; i < arg1.size(); ++i)
  11. ret.push_back(fnc(arg1[i], args[i]...));
  12. return ret;
  13. }
  14.  
  15. char f(std::string s, int p)
  16. {
  17. return s.at(p);
  18. }
  19.  
  20. template<int N>
  21. char fn(std::string s)
  22. {
  23. return s.at(N);
  24. }
  25.  
  26. int main() {
  27. // один map
  28. std::vector<std::string> a = {"LISP", "SICP", "THE", "BEST", "MUST", "HAVE"};
  29. std::vector<int> b = {2, 2, 1, 1, 0, 3};
  30. for(auto c : map(f, a, b))
  31. std::cout << c;
  32. std::cout << std::endl;
  33. // map от map
  34. std::vector<std::vector<std::string>> A = {{"IT", "SO", "TRUE"}, {"THAT", "NEVER", "ABANDON"}, {"THE", "LISP", "SITE"}};
  35. std::vector<char(*)(std::string)> F = {fn<0>, fn<1>, fn<2>};
  36. for(auto list : map(map<char, std::string>, F, A))
  37. for(auto c : list)
  38. std::cout << c;
  39. }
Success #stdin #stdout 0s 4684KB
stdin
Standard input is empty
stdout
SCHEME
ISTHEBEST