fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. typedef std::vector<std::vector<int>> IntMatrix;
  7.  
  8. void print_matrix(IntMatrix& mat)
  9. {
  10. std::cout << "[ ";
  11. std::for_each(mat.begin(), mat.end(), [](const std::vector<int>& row)
  12. {
  13. std::cout << " [ ";
  14. std::for_each(row.begin(), row.end(), [](int i)
  15. {
  16. std::cout << i << " ";
  17. });
  18. std::cout << "]\n";
  19. });
  20. std::cout << "]\n";
  21. }
  22.  
  23. int main()
  24. {
  25. IntMatrix mat{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
  26. std::cout << "Before:\n";
  27. print_matrix(mat);
  28. mat.resize(2);
  29. std::for_each(mat.begin(), mat.end(), [](std::vector<int>& v)
  30. {
  31. v.resize(2);
  32. });
  33. std::cout << "After:\n";
  34. print_matrix(mat);
  35. return 0;
  36. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Before:
[   [ 1 2 3 ]
  [ 4 5 6 ]
  [ 7 8 9 ]
]
After:
[   [ 1 2 ]
  [ 4 5 ]
]