我知道如何像这样初始化一维向量
int myints[] = {16,2,77,29};
std::vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
假设我有二维数据。
float yy[][2] = {{20.0, 20.0}, {80.0, 80.0}, {20.0, 80.0}, {80.0, 20.0}};
如何初始化一个二维向量?
在当前的C++(自2011年起)中,您可以在构造函数中初始化这样的向量:
vector<vector<float>> yy
{
{20.0, 20.0},
{80.0, 80.0},
{20.0, 80.0},
{80.0, 20.0}
};
实时查看代码演示:http://ideone.com/GJQ5IU。
如果您不事先知道值,仍然可以使用初始值初始化2D向量。
vector< vector <int> > arr(10, vector <int> (10, 0));
auto yy = std::vector<std::vector<float>>
{
{20.0, 20.0},
{80.0, 80.0},
{20.0, 80.0},
{80.0, 20.0}
};
float yy = {{..}}
是无效的。而我的编辑并不会使已发布的答案无效。 - Jarod42