jack06215

[tools] python zip() function

Jul 11th, 2020 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <functional>
  6. #include <iterator>
  7.  
  8. template <typename Iterator>
  9. void advance_all (Iterator & iterator) {
  10.     ++iterator;
  11. }
  12. template <typename Iterator, typename ... Iterators>
  13. void advance_all (Iterator & iterator, Iterators& ... iterators) {
  14.     ++iterator;
  15.     advance_all(iterators...);
  16. }
  17. template <typename Function, typename Iterator, typename ... Iterators>
  18. Function zip (Function func, Iterator begin, Iterator end, Iterators ... iterators)
  19. {
  20.     for(;begin != end; ++begin, advance_all(iterators...))
  21.         func(*begin, *(iterators)... );
  22.    
  23.     return func;
  24. }
  25.  
  26. int main()
  27. {
  28.     auto println = [](const char *message) { std::cout << "\n" << message << '\n'; };
  29.     auto lambda_echo = [](int i) { std::cout << std::setw(3) << i; };
  30.    
  31.     std::vector<int> col{ 23, 23, 37, 42, 23, 23, 37 };
  32.     std::vector<bool> another_col{ false, true, false, false, false, true, true };
  33.     std::vector<double> yet_another_col{ 68.5, 23.234, 68.1, 6.2456, 10.0, 11.112, 98.234 };
  34.  
  35.     auto print_func = [](auto i, auto j, auto k) {
  36.         std::cout << "element: " << i << " " << j << " " << k << std::endl;
  37.     };
  38.     zip(print_func, col.begin(),col.end(),another_col.begin(), yet_another_col.begin());
  39.     /*
  40.         element: 23 0 68.5
  41.         element: 23 1 23.234
  42.         element: 37 0 68.1
  43.         element: 42 0 6.2456
  44.         element: 23 0 10
  45.         element: 23 1 11.112
  46.         element: 37 1 98.234
  47.     */
  48.     return 0;
  49. }
Add Comment
Please, Sign In to add comment