简体   繁体   English

如何初始化std :: vector数组?

[英]how to initialize an array of std::vector?

As we all know, it's normal to initialize an array of int like this: 众所周知,像这样初始化一个int数组是正常的:

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): 因此,我想知道,如何in the same way初始化std :: vector数组(仅在初始列表中):

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: 我知道将代码编写为合法是合法的,现在我的问题是如何在一行代码中初始化vecAry:

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

In C++11, 在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 : 但是如果没有像Boost Assignment库中那样的某种辅助工具,就无法使用任意值来初始化它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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