fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main() {
  5. auto m = 2; // #rows
  6. auto n = 3; // #cols
  7. // row-major vector
  8. auto x = std::vector<double>{1,2,3,4,5,6};
  9.  
  10. auto const colIndex = 1;
  11. auto const col = std::vector<double>{7,8};
  12. // insert column {7,8} into the 2nd position
  13. // =>{1,7,2,3,4,8,5,6}
  14.  
  15. {
  16. x.resize(x.size() + col.size());
  17.  
  18. // we'll start shifting elements over from the right
  19. double *from = &x[m * n];
  20. const double *src = &col[m];
  21. double *to = from + m;
  22.  
  23. size_t R = n - colIndex; // number of cols left of the insert
  24. size_t L = colIndex; // number of cols right of the insert
  25.  
  26. while (to != &x[0]) {
  27. for (size_t i = 0; i < R; ++i) *(--to) = *(--from);
  28. *(--to) = *(--src); // insert value from new column
  29. for (size_t i = 0; i < L; ++i) *(--to) = *(--from);
  30. }
  31. };
  32.  
  33. for(auto const& xi : x)
  34. std::wcout << xi << L" ";
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 4348KB
stdin
Standard input is empty
stdout
1 7 2 3 4 8 5 6