stl vector创建二维数组

时间:2022-10-05 14:01:06
 vector<vector<int>> v();
for (auto it = v.begin(); it != v.end(); it++)
{
static int num = ;
(*it).reserve();//预留空间为5,但此时vector<int>的元素数量为0,下面这段jt=end()
/* for (auto jt = (*it).begin(); jt != (*it).end(); jt++,num++)
{
*jt = num;
}*/
for (int j = ; j < (*it).capacity(); j++, num++)
(*it).push_back(num);
}
for (auto it = v.begin(); it != v.end(); it++)
{ for (auto jt = (*it).begin(); jt != (*it).end(); jt++)
{
cout << *jt << " ";
}
}
for (int i = ; i < v.size(); i++)
{
for (int j = ; j < v[].size(); j++)
{
cout << v[i][j] << " ";
}
}

创建了一个5*5的二维数组,如果需要还可以扩容;行1声明了含有5个vector<int>的元素,每个元素已经用改类型的默认初始化方式初始化了,vector<int>的默认初始化为一个空vector;  cout << v[0].size() << " " << v[0].capacity() << endl;