I was creating a vector of strings of size 4x4 with all characters as dots i.e. I was creating:
....
....
....
....
And then I had to push this vector of strings in a vector of vector of strings like in the code below:
int main()
{
vector<vector<string>> ans;
int n=4;
vector<string> matrix(n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
matrix[i][j]='.'; //inserting '.' as characters
}
}
ans.push_back(matrix);
for(int i=0;i<n;i++) //printing the just inserted matrix
{
for(int j=0;j<n;j++)
{
cout<<ans[0][i][j]<<" ";
}
cout<<endl;
}
}
This was when I am printing it back, it gives garbage/nothing. But, I change the insertion matrix[i][j]='.';
to matrix[i]+='.';
, it is working fine.
int main()
{
vector<vector<string>> ans;
int n=4;
vector<string> matrix(n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
matrix[i]+='.'; //inserting '.' by +=
}
}
ans.push_back(matrix);
for(int i=0;i<n;i++) //printing the just inserted matrix
{
for(int j=0;j<n;j++)
{
cout<<ans[0][i][j]<<" "; //works fine
}
cout<<endl;
}
}
What is the cause of this behaviour?