fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. vector<int> interlace_vectors(const vector<int>& v1, const vector<int>& v2)
  7. {
  8. vector<int> result;
  9. for(size_t n = 0; n < max(v1.size(), v2.size()); ++n)
  10. {
  11. if(n < v1.size()) result.push_back(v1[n]);
  12. if(n < v2.size()) result.push_back(v2[n]);
  13. }
  14. return result;
  15. }
  16.  
  17. int main()
  18. {
  19. vector<int> v1 = {4,7,9,3,1};
  20. vector<int> v2 = {3,6,9};
  21. vector<int> v3 = interlace_vectors(v1, v2);
  22.  
  23. // for(int n: v3)
  24. // cout << n << ' ';
  25. // LWS is down at the moment, IDEone has an old compiler, so another way to loop:
  26. for_each(v3.begin(), v3.end(), [](int n){cout << n << ' ';});
  27. cout << '\n';
  28.  
  29. }
Success #stdin #stdout 0s 3060KB
stdin
Standard input is empty
stdout
4 3 7 6 9 9 3 1