Initialize empty vector of int by default?

There is such code:
vector<int> vec;
vector[2] = 12;


Why is it valid? Because we did not specify size, it should be by default to be completely empty?
March 23rd 20 at 19:04
2 answers
March 23rd 20 at 19:06
Solution
The code compiles, but it does not follow that it is valid.
In this case, there is UB and the program will behave unpredictably.
@kayla_Kozey , it will be very informative if the indication of undefined behavior are also given and explanations with reference to why in this case the behavior is undefined.
The theme of undefined behavior is quite extensive and specific design can simultaneously fall under some terms of undefined behavior. - danika_Wol commented on March 23rd 20 at 19:09
March 23rd 20 at 19:08
He's just compiled as
vector<int> vec;
int& tmp = vec.operator [] (2);
operator = (tmp, 12);

So everything is valid.
@bridget_Kiehn , and you have proof that your example code asimptotice author?
It would be very interesting to see the proof. - danika_Wol commented on March 23rd 20 at 19:11
what does that mean? - Catalina_West31 commented on March 23rd 20 at 19:14
Int& tmp = ... - Xzavier commented on March 23rd 20 at 19:17
@Xzavier, right, fixed - bridget_Kiehn commented on March 23rd 20 at 19:20

Find more questions by tags C++