#include #include #include // pair #include #include using namespace std; using PointT = pair; using BoardT = vector>; ostream & operator << (ostream & s, PointT p); void PrintBoard(const BoardT & board); int main() { int row = 3; int col = 4; int i,j; //vector> board(row, vector(col,PointT())); // method 1, wicked cool c++ way BoardT board(row, vector(col,PointT())); // method 2, the way I can remember but don't like. BoardT board2(row); for(i = 0; i < row; i++) { board2[i] = vector(col); } for(i = 0; i < row; i ++) { for (j = 0; j < col; j++) { PointT value{i,j}; board[i][j] = value; board2[i][j] = value; } } PrintBoard(board); cout << endl; PrintBoard(board2); cout << endl; return 0; } ostream & operator << (ostream & s, PointT p){ s << "(" << p.first << "," << p.second << ")"; return s; } void PrintBoard(const BoardT & board){ size_t row, col; // print the header cout << " "; for(col = 0; col < board[0].size(); ++col) { cout << setw(5) << col ; } cout << endl; // print the line under the header cout << " +"; cout << setfill('-'); cout << setw(static_cast(board[0].size()+1)*5-2) << " " << endl; cout << setfill(' '); for(row = 0; row < board.size(); ++row ) { cout << setw(2) << row << "|"; for (col = 0; col < board[0].size(); ++col) { cout << board[row][col] << " "; } cout << endl; } return; }