为什么int类型的二维数组初始化是连续的,而二维向量则是按数字集初始化的?
这里是结果:
int main()
{
array<array<int, 2>, 2> td{ 2, 6, 4, 8 }; //array of arrays
for (int i = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
cout << td[i][j] << ' ';
cout << endl;
vector<vector<int>> vtd{ { 5, 1 }, { 0, 2 } }; //vector of vectors
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
cout << vtd[i][j] << ' ';
return 0;
}
这里是结果:
2 6 4 8
5 1 0 2
std::array
的初始化提供警告,因为它确实缺少花括号。 - Some programmer dude{{0,1},{2,3}}
进行初始化,会出现编译错误。然后我使用了{0,1,2,3}
,它可以正常工作。 - Kenneth Bastian