如何初始化 std::vector 数组? [英] how to initialize an array of std::vector?

查看:50
本文介绍了如何初始化 std::vector 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,像这样初始化一个int数组是很正常的:

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

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

所以,我想知道,如何以相同的方式初始化 std::vector 数组(仅在初始列表中):

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};

我知道将代码编写为流程是合法的,现在我的问题是如何在一行代码中初始化 vecAry:

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};

推荐答案

在 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()};

但是如果没有像 Boost 赋值库:

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

这篇关于如何初始化 std::vector 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆