Guest User

Untitled

a guest
Sep 26th, 2020
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4.  
  5. template<typename T>
  6. void dump(std::vector<T> keys)
  7. {
  8.     std::wcout << L"Keys: [";
  9.     auto isFirst = true;
  10.     for (auto key : keys)
  11.     {
  12.         std::wcout << (isFirst ? L"" : L", ") << key;
  13.         isFirst = false;
  14.     }
  15.     std::wcout << L"]" << std::endl;
  16. }
  17.  
  18. template<typename T1, typename T2> using Mapper = std::function<T2(T1 item)>;
  19.  
  20. template<typename T1, typename T2>
  21. std::vector<T2> map(const Mapper<T1, T2> mapper, const std::vector<T1> vec)
  22. {
  23.     std::vector<T2> result;
  24.     for (T1 x : vec)
  25.     {
  26.         result.push_back(mapper(x));
  27.     }
  28.     return result;
  29. }
  30.  
  31. using namespace std::placeholders;
  32.  
  33. int main()
  34. {
  35.     auto multiplier = 5;
  36.     auto func = [](int n, int m)
  37.     {
  38.         return n * m;
  39.     };
  40.     std::function<int(int)> mapper = std::bind(func, _1, multiplier);
  41.     auto list = map<int, int>(
  42.         mapper,
  43.         { 1, 2, 3 }
  44.     );
  45.     dump<int>(list);
  46. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×