# Why this initial vector<vector<int> > is wrong?

• ``````class Solution {
public:
vector<vector<int> > generateMatrix(int n)
{
//int row=matrix.size();//row num

vector<vector<int> > matrix(n,n);
//vector<int> spiralnum;
//if(row<=0) return spiralnum;
//int col=matrix.at(0).size();//column num
//if(col<=0) return spiralnum;

int row=n,col=n;
int start=0;//row start, and col start

//bool PrintNum=false;
int k=1;
while(1)
{
int colend=col-1-start;
int rowend=row-1-start;
if(start>colend || start> rowend)
break;

if(start<=rowend)//at least one row, maybe none number left
{
for(int j=start;j<=colend;j++)//row: start col: start->colend, if col has num, start must be valid
{
matrix.at(start).at(j)=k;
k++;

//spiralnum.push_back(matrix.at(start).at(j));
}
}

if(start<=colend)//at least one row
{
for(int i=start+1;i<=rowend;i++)//col: colend row:start+1->colend
{
matrix.at(i).at(colend)=k;
k++;
//spiralnum.push_back(matrix.at(i).at(colend));
}
}

if(start+1<=rowend)//at least two rows
{
for(int j=colend-1;j>=start;j--)//row: rowend col: colend-1->start
{
matrix.at(rowend).at(j)=k;
k++;
//spiralnum.push_back(matrix.at(rowend).at(j));
}
}
if(start+1<=colend)//at least two rows
{
for(int i=rowend-1;i>=start+1;i--)//col: start row: rowend-1->start+1
{
matrix.at(i).at(start)=k;
k++;
//spiralnum.push_back(matrix.at(i).at(start));
}
}

start++;

}
return matrix;
}
};
``````

I tried vector<vector<int> >matrix (n, vector<int>(n)), and it is correct,
vector<vector<int> > matrix(n,n); has been accepted by VS2010 and gcc3.2.3, but not by gcc4.4.7

Could anybody explain vector<vector<int> > 's using tips ? Thanks.

• This post is deleted!

• no idea ? does it mean that the gcc 4.4.7 use a diffirent tool?

• yeah...maybe vector<vector >matrix (n, vector(n)) will be more general

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.