That’s because constructor of A
is called with N=0
. When you input N
, it doesn’t affect the vector at all. If you really want to keep using globals, then use std::vector::resize
but the recommended way would be:
int main(){
std::size_t n;
std::cin >> n;
std::vector<int> A(n);
for(auto & x : A) std::cin >> x;
}
3
solved How to input to vector correctly in C++