简体   繁体   中英

how to initialize an array of std::vector?

As we all know, it's normal to initialize an array of int like this:

int intAry[] = {7, 8, 9};

So, I want to know, how can initialize an array of std::vector in the same way (just in the initial list):

typedef std::vector<int> type;
type vecAry[] = {vec1, vec2, vec3};

I know it's legal to write code as fllows, now my question how to initialize vecAry in one line of code:

type vec1;
type vec2;
type vec3;
type vecAry = {vec1, vec2, vec3};

In C++11,

type vecAry[] {{},{},{}};

or if you want non-empty vectors

type vecAry[] {{1,2,3},{4,5,6},{7,8,9}};

If you're stuck in the past, you can initialise it with empty vectors:

type vecAry[] = {type(), type(), type()};

but you can't initialise it with arbitrary values without some kind of helper like those in the Boost Assignment library :

type vecAry[] = {list_of(1)(2)(3), list_of(4)(5)(6), list_of(7)(8)(9)};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM