fork(7) download
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. template<typename T, typename U>
  9. std::vector<U> map(const std::vector<T> &v, std::function<U(const T&)> f) {
  10. std::vector<U> res;
  11. res.reserve(v.size());
  12. std::transform(std::begin(v), std::end(v), std::inserter(res, res.begin()), f);
  13. return res;
  14. }
  15.  
  16.  
  17. int main() {
  18. vector<int> numbers = { 1, 3, 5 };
  19.  
  20. //vector<string> strings = map(numbers, [] (int x) { return string(x,'X'); });
  21.  
  22. vector<string> strings = map<int, string>(numbers, [] (int x) { return string(x,'X'); });
  23.  
  24. for (const string& s : strings) {
  25. cout << s << "\n";
  26. }
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
X
XXX
XXXXX