Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not allowed to use variable-length arrays as vector element?

Tags:

c++

For example:

#include<vector>
using namespace std;
int main()
{
   vector<int[]> vec;//serious compiler error
   vector<int[2]> vec={{1,2}};//error:array must be initialized with a brace-enclosed initializer
}

In addition, how to rectify the grammar of the second one? I already use a brace-enclosed initializer.

like image 411
scottxiao Avatar asked Dec 18 '22 20:12

scottxiao


1 Answers

It's not a variable-length array, those do not exist in C++. It's an array without a size specifier, an incomplete type that doesn't meet the requirements of most (all?) vector operations.

The second attempt tries to copy c-arrays (list initialization always does copying), and that too is not supported.

If you want a vector of arrays, spell it as std::vector<std::array<int, 2>>.

like image 159
StoryTeller - Unslander Monica Avatar answered May 12 '23 15:05

StoryTeller - Unslander Monica