fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <functional>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <iterator>
  8.  
  9. using namespace std;
  10.  
  11. template <template<class...> class Collection, typename Lambda, typename Type, typename ...CollectionArgs>
  12. auto map(const Collection<Type, CollectionArgs...> &source, Lambda&& lambda)
  13. -> Collection< decltype( lambda(*source.begin()) ) >
  14. {
  15. Collection< decltype( lambda(*source.begin()) ) > result;
  16. std::transform(source.begin(), source.end(), std::inserter(result, result.end()), lambda);
  17. return result;
  18. }
  19.  
  20. int main() {
  21.  
  22. std::vector<int> ints = {1,2,3,4,5};
  23. std::set<int> int_set = {7,8,9};
  24. std::vector<float> floats = map( ints, []( int v) -> float { return v;} );
  25. std::set<float> floats2 = map( int_set, []( int v) -> float { return v;} );
  26. for( auto & n: floats)
  27. {
  28. std::cout << n << std::endl;
  29. }
  30.  
  31. std::cout << "---" << std::endl;
  32. for( auto & n: floats2)
  33. {
  34. std::cout << n << std::endl;
  35. }
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 5512KB
stdin
Standard input is empty
stdout
1
2
3
4
5
---
7
8
9