Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize 2d vector using constructor in c++?

Tags:

c++

vector

I know how to initialize 1d vector like this

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

suppose I have 2d data.

float yy[][2] = {{20.0, 20.0}, {80.0, 80.0}, {20.0, 80.0}, {80.0, 20.0}};

How can I initialize a 2d vector ?

like image 428
Jaeger Avatar asked Dec 16 '22 01:12

Jaeger


1 Answers

In current C++ (since 2011) you can initialize such vector in constructor:

vector<vector<float>> yy
{
    {20.0, 20.0},
    {80.0, 80.0},
    {20.0, 80.0},
    {80.0, 20.0}
};

See it alive: http://ideone.com/GJQ5IU.

like image 122
herohuyongtao Avatar answered Jan 01 '23 17:01

herohuyongtao