#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<std::vector<int>> vvi = {{1, 5, 3, 4}, {0, 4, 1, 0}, {7, 2, 3, 1}};
  int mx, my;

  mx = vvi[0].size();
  my = vvi.size();
  std::cout << "X方向のサイズ = " << mx << ", Y方向のサイズ = " << my << std::endl;

  std::vector<int> wrk;

  for (std::vector<int>& v : vvi)
    std::copy(std::begin(v), std::end(v), std::back_inserter(wrk));

  std::for_each(std::begin(wrk), std::end(wrk), [](int i){ std::cout << i << ' '; });
  std::cout << std::endl;
  std::sort(std::begin(wrk), std::end(wrk));
  std::for_each(std::begin(wrk), std::end(wrk), [](int i){ std::cout << i << ' '; });
  std::cout << std::endl;

  for (int i = 0; i < my; i++) {
    for (int j = 0; j < mx; j++) {
      vvi[i][j] = wrk[i * mx + j];
      std::cout << vvi[i][j] << ' ';
    }
    std::cout << std::endl;
  }
}