Creating a grid with rows and columns using vectors

I want to create a grid with empty rows and columns and I did the following function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
grid(unsigned row, unsigned col)
	{
		vector < vector<int>>stuff;
		for (int i = 0; i < row; i++)
		{
			vector<int>temp;
			for (int j = 0; j < col; j++)
			{
				temp.push_back(i);
			}
			stuff.push_back(temp);
		}

	}


But my above function is not working and is not creating a grid of empty rows and empty columns.

Can anyone help me.
Last edited on
If you want "empty" vectors then why are you using push_back() which inserts that value?

If all you want is an empty vector then why not initialize the vector as "empty"?

1
2
    size_t rows = 6, cols = 10;
    std::vector<std::vector<int>> stuff(rows, std::vector<int>(cols));
@jlb, It's not really "empty". It's just initialized to all zeroes. It still has a total of rows*cols elements in use (i.e., stuff.size()==rows and stuff[0 to rows-1].size()==cols).
It's not really "empty". It's just initialized to all zeroes.

Correct, it is default initialized. A vector element can never be truly "empty".

Your vector stuff is local to the grid function. When you exit grid(), stuff goes out of scope and goes away. So, how do you know whether or not the grid is created or not?

Topic archived. No new replies allowed.