fork download
  1. #include <vector>
  2. #include <tuple>
  3. #include <algorithm>
  4. #include <type_traits>
  5.  
  6. template<class T>
  7. using Invoke = typename T::type;
  8.  
  9. template<class T>
  10. using Unqualified = Invoke<std::remove_cv<Invoke<std::remove_reference<T>>>>;
  11.  
  12. template<class T>
  13. using ValueType = typename Unqualified<T>::value_type;
  14.  
  15. template<class T>
  16. T const& forward_index(std::vector<T> const& v, unsigned i){
  17. return v[i];
  18. }
  19.  
  20. template<class T>
  21. T&& forward_index(std::vector<T>&& v, unsigned i){
  22. return std::move(v[i]);
  23. }
  24.  
  25. template<class... Vs>
  26. std::vector<std::tuple<ValueType<Vs>...>> zip(Vs&&... vs){
  27. auto lo = std::min({vs.size()...});
  28. std::vector<std::tuple<ValueType<Vs>...>> v;
  29. v.reserve(lo);
  30. for(unsigned i = 0; i < lo; ++i)
  31. v.emplace_back(forward_index(std::forward<Vs>(vs), i)...);
  32. return v;
  33. }
  34.  
  35. #include <iostream>
  36.  
  37. int main(){
  38. std::vector<int> a{1,3,5}, b{2,4,6}, c{3,5,7};
  39. for(auto&& t : zip(a, b, c)){
  40. std::cout << std::get<0>(t) << ":" << std::get<1>(t) << ":" << std::get<2>(t) << "\n";
  41. }
  42. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
1:2:3
3:4:5
5:6:7