一次性创建和初始化一组向量 [英] Create and initialise an array of vectors in one go

查看:31
本文介绍了一次性创建和初始化一组向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要创建一个字符串数组,这有效:

To create an array of strings this works:

std::string array[] = {
    "first",
    "second",
    :
    "last"
};

如果我尝试对向量做同样的事情,它就不起作用:

If I try to do the same with vectors it doesn't work:

std::vector<int> array[] = {
    {1, 2},
    {3, 4, 5}
    :
    {9}
};

我得到不能用初始化列表初始化非聚合".

I get "non-aggregates cannot be initialized with initializer list".

初始化向量数组的正确语法是什么?

What is the correct syntax to initialise the array of vectors?

请注意,我需要像这样一次性完成,而不是使用向量成员函数一次一个地用 int 填充向量(因为我正在尝试设置一个文件这将在编译时根据初始化数字创建数组,因此在这种情况下调用成员函数无济于事).

Note that I need to do it in one go like this and not using vector member functions to fill the vectors with ints one at a time (because I'm trying to set up a file that will create the array at compile time based upon the initialisation numbers, so calling member functions doesn't help in this case).

推荐答案

尽管使用向量数组的目的非常值得怀疑(那为什么不使用向量向量或二维数组呢?),但要做什么?您希望正确的语法如下所示:

Although the purpose of using an array of vectors is very much questionable (why not use vector of vectors or a two-dimentional array then?), to do what you want the correct syntax would look like:

std::vector<int> array[] = {
    vector<int>(2),
    vector<int>(3),
    //...
    vector<int>(1)
};

它没有任何意义的原因是因为在这种情况下它在语义上与创建指针数组相同:

The reason it doesn't make any sense, is because in this case it's semantically the same as creating an array of pointers:

int* array[] = {
        new int[0],
        new int[0],
        //...
        new int[0],
    };

唯一的区别是内部 std::vector 自己管理这些指针的生命周期.这意味着除非您正在创建一个恒定大小的向量(即,向量不可能重新分配其内部存储器,这是可能的,如果您将向量声明为常量或只是确保您永远不要在上面调用 resize、reserve、push_back、insert、erase 和其他类似的方法),您将有效地获得一个内部带有指针的小对象数组.这就是向量通常的实现方式.

with the only difference is that internally std::vector is managing the lifetime of these pointers all by itself. That means that unless you're creating a constant-size vector (i.e. without a possibility for the vector to have its internal memory re-allocated, which is possible, if you're either declaring the vector to be const or simply make sure you don't ever call resize, reserve, push_back, insert, erase and other similar methods on it), you're going to get effectively an array of small objects with a pointer inside. This is how vector is usually implemented.

不清楚,在编译时创建数组是什么意思.以您尝试做的方式声明的数组,以及这些:

It's not clear, what you mean by creating an array on compile time. Arrays declared in the manner you try to do, as well as these:

int fixed_size_array[100];

确实是以这种方式创建的,而不是它们的大小是在编译时确定的.但是向量是一个不同的野兽——它们是复杂的 C++ 对象,它们在运行时动态管理它们的内存,这意味着它们在编译时根本无法设计为固定大小.

are indeed created in such a way, than their size is determined on compile time. But vectors are a different beast -- these are complex C++ objects, which manage their memory dynamically in run-time, which means that they simply cannot have a compile-time fixed size by design.

这篇关于一次性创建和初始化一组向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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