0

fill_n not working

Hi Please refer code below: I have at the end of code added fill_n code lines but vector is not getting updated... Please suggest. Do I need to do push_back all the time and fill_n does not work this way? https://code.sololearn.com/cd3ezhtwAqKI/?ref=app

20th Oct 2022, 6:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Réponses
0
What you really want is instead of using reserve(), you should use resize(). Reserve: https://cplusplus.com/reference/vector/vector/reserve/ Resize: https://cplusplus.com/reference/vector/vector/resize/
20th Oct 2022, 7:17 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
+ 1
Yes, the reserve() does only change the max value of size, so the size doesn't change, if you try to fill_n more than the size it will not change the vector.
20th Oct 2022, 7:51 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
0
Thanks Tomás Ribeiro It works as per my need with reset I dont have to use fill_n also Now my work is done but curious to know that whats the use of fill_n ? Does it work only on available elements and it cant add new elements alltogether ? https://code.sololearn.com/cN0xHypY26lT/?ref=app
20th Oct 2022, 7:42 AM
Ketan Lalcheta
Ketan Lalcheta - avatar