fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4.  
  5. using Inner = std::vector<int>;
  6. using Outer = std::vector<Inner>;
  7.  
  8. std::ostream& operator <<(std::ostream& os, const Inner& row)
  9. {
  10. for (auto col : row)
  11. std::cout << col << ' ';
  12. return os;
  13. }
  14.  
  15. std::ostream& operator <<(std::ostream& os, const Outer& tbl)
  16. {
  17. for (auto const& row : tbl)
  18. std::cout << row << '\n';
  19. return os;
  20. }
  21.  
  22. int main()
  23. {
  24. Inner inner;
  25. for (int i=1; i<=10; ++i)
  26. inner.emplace_back(i);
  27.  
  28. Outer outer(10, inner);
  29.  
  30. // iterators
  31. for (auto row = outer.begin(); row != outer.end(); ++row)
  32. {
  33. for (auto col = row->begin(); col != row->end(); ++col)
  34. std::cout << *col << ' ';
  35. std::cout.put('\n');
  36. }
  37. std::cout.put('\n');
  38.  
  39. // ranged-for
  40. for (auto const& row : outer)
  41. {
  42. for (auto col : row)
  43. std::cout << col << ' ';
  44. std::cout.put('\n');
  45. }
  46. std::cout.put('\n');
  47.  
  48. // stream overloads
  49. std::cout << outer << '\n';
  50. }
  51.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 

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

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