jack06215

[tutorial] more stl-ish map()

Jul 12th, 2020 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <functional>
  6. #include <iterator>
  7. #include <list>
  8.  
  9. template<typename RET_CONT, typename IT, typename OPERATION>
  10. RET_CONT map(IT begin, IT end, OPERATION op)
  11. {
  12.    RET_CONT ret;
  13.    std::transform(begin, end, back_inserter(ret), op);
  14.    return ret;
  15. }
  16.  
  17. int main()
  18. {
  19.     auto println = [](const char *message) { std::cout << "\n" << message << '\n'; };
  20.     std::vector<int> col{ 23, 23, 37, 42, 23, 23, 37 };
  21.     typedef std::vector<double> astype;
  22.    
  23.     auto func = [] (auto i) {
  24.         return i * 10.0;
  25.     };
  26.     astype out = map<astype>(col.begin(), col.end(), func);
  27.     std::copy(out.begin(), out.end(), std::ostream_iterator<decltype(out.at(0))>(std::cout, " "));
  28.  
  29.     return 0;
  30. }
Add Comment
Please, Sign In to add comment