0

this is my code:

int N;
cin >> N;
vector<int> v[N];// get problem here

In 3rd line the editor said that :"expression must have a constant valueC/C++(28) vector_arry.cpp(14, 19): the value of variable "N" (declared at line 12) cannot be used as a constant"

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Size of array must be a compile time constant. So you cannot write `int N; cin >> N; int arr[N];`. – Jason Jul 12 '22 at 06:27
  • 5
    vector v(N) – alon Jul 12 '22 at 06:28
  • Please don't tag unrelated tags, like the C language tag for a C++ coding question. – Some programmer dude Jul 12 '22 at 06:29
  • 1
    As for the problem, it's most likely because you think that vectors are similar to arrays to the point of defining a vector is done exactly like for arrays, which it isn't. You attempt to define an *array* of `N` vectors, not a single vector of `N` elements. – Some programmer dude Jul 12 '22 at 06:29
  • Taking your code literally, it seems you want `vector> v(N);` -- that is a vector initialized with `N` empty vectors. If that's not what you intended, then add more detail to your question. – paddy Jul 12 '22 at 06:30
  • ALmost certainly you should have written `vector v(N);`, `()` not `[]`. – john Jul 12 '22 at 06:40

0 Answers0