Is it possible to inset values in a std::vector in an non-consecutive manner like,
std::vector<int> Myvector;
//Myvector[0] = 123;
//Myvecotr[2] = 456;
//Myvector[5] = 789;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When your code uses an index for an element that does not yet exist. Or in other words, if your index exceeds (>=)
vector::size()then you are accessing a non existent element. Alas you end up in undefined-behaviour.So you need to add those elements
Or as stated in other answers you set the size (which I’d prefer):
Or state the size from begin on: