Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my vector need a constant value?

I'm trying to make an adjacency list graph implementation using vectors. In my vector declaration, I keep getting an error of "expression must have a constant value." I don't understand why I get this error, as I thought that vectors were already dynamic arrays and this wouldn't be a problem.

int nodes = 5;

vector<int> adjacencyList[nodes];
like image 937
Jake Orantes Avatar asked Dec 21 '25 15:12

Jake Orantes


1 Answers

The size of an array variable must be compile time constant, regardless of the type of the element of the array.

It is somewhat unclear whether your array declaration was intentional. If you want a dynamic array of vectors, then what you could use instead is a vector of vectors. If your intention was to create a single vector, then don't use the square brackets since the square brackets are syntax for an array. Here is an example of how to initialise a single vector of particular size:

std::vector<int> adjacencyList(nodes);
like image 160
eerorika Avatar answered Dec 24 '25 09:12

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!