如何在c ++中使用构造函数初始化2d向量?

Jae*_*ger 3 c++ vector

我知道如何初始化这样的1d矢量

int myints[] = {16,2,77,29};
std::vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
Run Code Online (Sandbox Code Playgroud)

假设我有2d数据.

float yy[][2] = {{20.0, 20.0}, {80.0, 80.0}, {20.0, 80.0}, {80.0, 20.0}};
Run Code Online (Sandbox Code Playgroud)

如何初始化2d向量?

her*_*tao 6

在当前的C++中(自2011年起),您可以在构造函数中初始化此类向量:

vector<vector<float>> yy
{
    {20.0, 20.0},
    {80.0, 80.0},
    {20.0, 80.0},
    {80.0, 20.0}
};
Run Code Online (Sandbox Code Playgroud)

看到它活着:http://ideone.com/GJQ5IU.