vect
does not have allocated storage, so there isn't an arena to put the values on, hence undefined behaviour. You can get the allocated space of a container with capacity(), so in your case, vect.capacity()
will return 0.
Furthermore, operator [] won't check the boundaries, so it will try to access to [ 2 ], even if its size is 0. To access checking boundaries use at()
.
Considering that, to solve the problem you will have to allocate space for the container (in your case, std::vector
), that is, reserve an area of memory just for vect
, vector
offers different ways to modify its size, such as constructor, push_back, insert, resize, etc.
Note: if possible, avoid the use of using namespace std
and <bits/stdc++.h>