Create and initialise an array of vectors in one g

2019-04-11 09:36发布

问题:

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?

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).

回答1:

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

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

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.



回答2:

To make a long story short, it's simply not supported. There are various workarounds to get similar syntax (e.g. Boost::assign) but these don't change what you get in the executable -- they just make calling the member functions a bit simpler than a string of calls to push_back.



回答3:

Maybe something like this will take you in the direction you want?

const int arr[] = { 1, 2, 3, 4, 5 };

std::vector v( arr, arr + sizeof(arr) / sizeof(*arr) );


回答4:

to create a vector of vector code

std::vector< std::vector< int > > v;

calling the default no argument constructor create a Simple vector of int

int arry[] = {1,2,3,4,5};
std::vector< int > list(arry, arry + sizeof(arry) / sizeof(int));

Thus constructed a vector from an Integer array I'd recommend you to use push_back() Method Instead in your Scenario.

However If you just wanna get dirty you can follow the way mentioned above and create an array of std::vector< int > and pass it to the constructor of std::vector< std::vector< int > >

Check http://www.cplusplus.com/reference/stl/vector/vector/ for more Information You might be interested on this specific Constructor overload template <class InputIterator> vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );

Neel



标签: c++ vector