fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <string>
  6. #include <iterator>
  7.  
  8. typedef std::vector<std::vector<int>> Mat;
  9.  
  10. std::ostream& operator <<(std::ostream& os, const Mat& mat)
  11. {
  12. for (auto&& row : mat)
  13. {
  14. for (auto x : row)
  15. os << x << ' ';
  16. os << '\n';
  17. }
  18. return os;
  19. }
  20.  
  21. int main()
  22. {
  23. Mat m1, m2, *pm = &m1;
  24. std::string line;
  25.  
  26. while (std::getline(std::cin, line))
  27. {
  28. // switch matrix on '#' encounter
  29. if (line == "#")
  30. {
  31. pm = &m2;
  32. continue;
  33. }
  34.  
  35. // add line to current matrix
  36. std::istringstream iss(line);
  37. pm->emplace_back((std::istream_iterator<int>(iss)),
  38. std::istream_iterator<int>());
  39. }
  40.  
  41. std::cout << m1 << '\n';
  42. std::cout << m2 << '\n';
  43. }
Success #stdin #stdout 0s 3436KB
stdin
2 3 4 5 6 7
2 6 8 2 4 4
6 3 3 44 5 1
#
4 2 1 6 8 8
5 3 3 7 9 6
0 7 5 5 4 1
stdout
2 3 4 5 6 7 
2 6 8 2 4 4 
6 3 3 44 5 1 

4 2 1 6 8 8 
5 3 3 7 9 6 
0 7 5 5 4 1