+ 1

Why 6 calls to destructor for 5 objects?

I don’t understand why the output is 6, can someone please explain it? https://code.sololearn.com/cShhwiOB4219/?ref=app

30th Jun 2022, 9:31 PM
Edward Finkelstein
Edward Finkelstein - avatar
1 Answer
+ 6
It's because the maximum capacity of the vector has been reached. When you push_back a new value the vector has to reallocate a new, and bigger array to hold the new value. This involves copying the object and destructing the old. The 1 extra is just for a temporary in the constructor of the vector. It creates the object, copies it 4 times, then destructs it.
30th Jun 2022, 10:37 PM
Dennis
Dennis - avatar