fork download
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. typedef std::vector<std::vector<int>> vector2d;
  6.  
  7. vector2d CreateVector2d()
  8. {
  9. vector2d v2d(3, std::vector<int>(3));
  10.  
  11. for (unsigned i = 0; i < 3; ++i)
  12. v2d[i][i] = 1;
  13.  
  14. return v2d;
  15. }
  16.  
  17. void print(std::ostream& os, const vector2d& v)
  18. {
  19. // C++11:
  20. for (auto & vec : v)
  21. {
  22. for (auto & i : vec)
  23. os << std::setw(2) << i;
  24. os << '\n';
  25. }
  26.  
  27. // or:
  28.  
  29. //for (vector2d::const_iterator i = v.begin(); i != v.end(); ++i)
  30. //{
  31. // for (std::vector<int>::const_iterator j = i->begin(); j != i->end(); ++j)
  32. // os << std::setw(2) << *j;
  33. // os << '\n';
  34. //}
  35.  
  36. // or:
  37. //for (unsigned i = 0; i < v.size(); ++i)
  38. //{
  39. // for (unsigned j = 0; j < v[i].size(); ++j)
  40. // os << std::setw(2) << v[i][j];
  41. // os << '\n';
  42. //}
  43. }
  44.  
  45. int main()
  46. {
  47. print(std::cout, CreateVector2d());
  48. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
 1 0 0
 0 1 0
 0 0 1