#### How to modify vector of a vector from 3×3 to 4×4 using push_back and insert in c++?

A beginner to C++ programming here!

I’m working on a project of coding a two-player tic-tac-toe game. My grid is a 2D vector of a vector size 3×3 filled with dots in the beginning. The program asks x- and y-coordinates from the user and replaces the dot with the X or 0.

Now, my task is to increase the grid size in two-ways, for example when given coordinates x or y is greater by one than the size of the game board the board will expand to the right and downwards or if given coordinates for example x=2 and y=0, the board should expand left and upwards.

Here is how I tried to approach the situation, I made two functions:

Here you have my vector of a vector:

``````int grid_size = 3;
vector< vector<char>>grid(grid_size, vector<char>(grid_size, '.'));
``````

First one tries to deal with the down and right expansion:

``````void expand_grid_down_right(vector<vector<char>>&grid){
//Let's make some helpers
vector<char> grid_row;
vector<char> grid_col;
//Adds new row but not column
for(int i = 0; i < grid.size();i++){
grid_row.push_back('+');

}
vector<char> new_line(grid_row.size(), '+');
grid.push_back(new_line);

for(unsigned int i = 0; i < grid.size(); i++){
cout << (i+1)%10 << ' ';

}
cout << endl;
//Draws the grid with three rows of dots and one row of plus-signs
//Column numbered with four appears blank?
//If I try to touch this with at(i).at(j) it gives out of bounds error
for (auto &grid_row : grid) {

for (auto &cell : grid_row) {

cout << cell << ' ';
}

cout << endl;
}
//But this prints it as one column?
for(int i=0; i<grid.size();i++){
for(int j=0; j<grid[i].size(); j++){
cout << grid[i][j] << endl;
}
}
//I want to at to the new column the char = '.' but not sure what this does?
for(int i=0; i<grid.size();i++){
for(int j=0; j<grid[i].size(); j++){
grid.at(i).at(j) = '.';
}
}
}
``````

Second one tries to deal with the up and left expansion:

``````void expand_grid_up_left(vector<vector<char>>&grid){
unsigned int dimension = grid.size();
vector<char> grid_col;
//Add new row of plus-signs to the top row but does not shift
//So from 3x3 to 4x3 but how to get it 4x4?
for (int i=0; i<dimension; i++){
grid_col.insert(grid_col.begin(),'+');
}
vector<char> line(grid_col.size(), '+');
grid.insert(grid.begin(), line);
for (auto &grid_col : grid) {

for (auto &cell : grid_col) {

cout << cell << ' ';
}

cout << endl;
}
}
``````

But neither of them work in the way they are supposed to. I am using the plus-signs to see where the new lines are added. The down and right-expansion produces for rows and it draws an empty column 4. If I try to access this with index by index loop it just draws a single column with all of the dots and plus signs. The one that is supposed to expand left and up only puts a row up but doesn’t go to left.

Does the empty column 4 exist and how do I make them work so that the expansion becomes from 3×3 to 4×4 or in broader sense NxN?

Source: Windows Questions C++