[Solved] Why is my final vector double the size it should be and have leading 0’s?


vector<int> output_list(n * m); initializes output_list to be size n*m and fills with 0’s. You then push_back values after that.

You have 3 options:

  1. Don’t initialize output_list to have values. Let the program reserve space on the fly as you push_back.

  2. Don’t initialize output_list to have values, but increase its capacity using reserve.

  3. Initialize output_list as you have, but don’t push_back in the function. Instead, modify the values of the indexes instead of adding more.

5

solved Why is my final vector double the size it should be and have leading 0’s?