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:
-
Don’t initialize
output_list
to have values. Let the program reserve space on the fly as youpush_back
. -
Don’t initialize
output_list
to have values, but increase its capacity usingreserve
. -
Initialize
output_list
as you have, but don’tpush_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?