vector<int> v1; v1.reserve(3); v1[0] = 0; v1[1] = 1; v1[2] = 2;
This is probably undefined behavior (although not sure if it is implementation dependent).
You cannot use operator[] to populate a vector, because it returns a reference to the underlying object, which in your case is nothing more than a bunch of bits.
You must either use push_back() OR just resize your vector. Using the latter: -
vector<int> v1; v1.resize(3); v1[0] = 0; v1[1] = 1; v1[2] = 2;
ravi
source share