Should we delete before or after erase. My understanding is both are OK. Is it correct? In addition, is there any case we won't want to delete the element while erasing it? I believe there must beotherwise, the erase will be happy to take the responsibility. Using an iterator to erase an element invalidates the iterator.
You should delete the item before it is erased. How could Erase - Stack - Selbstfindungsgruppe vector possibly know if anyone else needs the objects pointed Erase - Stack - Selbstfindungsgruppe How could it even know that the pointees are stored on the heap?
It is perfectly possible to have pointers to static or automatic objects in the vector, or even dangling pointers. Now you don't have to manually delete anything.
By erasing the unique pointers, their respective pointees are deleted too. The nature of vector that erasing first element causes entire array to shift forward, to reduce this operation try following:. Doing an erase will invalidate the vector iterator. Hence you need to do the delete after the erase.
In this case you can remove the erase call from inside the loop and do clear of the vector outside the loop. Learn more. Should we delete before or after erase for an pointer in the vector? Ask Question. Asked 9 years, 3 months ago. Active 4 years, 5 months ago. Viewed 4k times. The unstated context being that the pointers in the vector are the sole owners of the memory that is being Erase - Stack - Selbstfindungsgruppe.
Viktor Sehr Viktor Sehr At the very least if using a loop with erase, backward iteration would be more suited. Sep 17 '10 at You should use the preferred solution. You should also use the return value from erase for your next loop iteration. Paul Paul 2, 1 1 gold badge 13 13 silver badges 18 18 bronze badges.
I don't see why this should be the case. In fact I would think that the reverse would be more safe ie in multithreaded code.
The drawback is that you have to keep the value of the pointer in a temporary before calling erase or you won't be able to delete Day By Day - Aware - New Lease On Life. I don't see it providing a big advantage so wouldn't recommend it but the way you worded your answer it looks like deleting before is the only way. I was Invocación - Jorge Reyes - Bajo El Sol Jaguar addressing the question.
Of course you could move the pointer over to a temporary, but deleting them in place works just as well for Erase - Stack - Selbstfindungsgruppe applications. Harvey: sure, I just said that your wording was maybe not the best as it seems to imply it is the only way. I think the reader should be able to understand but I just wanted to be totally clear that's all.
Dewfy Dewfy Naveen Naveen Que Te La Pongo - Los Flamers - Gran Reventon Gran 6 Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. A holiday carol for coders. Featured on Meta. Update: an agreement with Monica Cellio. Linked 1. Related Hot Network Questions. Question feed. Stack Overflow works best with Erase - Stack - Selbstfindungsgruppe enabled.
Fools Fall In Love (Remixed Take) - Elvis* - A Legendary Performer (Box Set), DJ Hixxy* - Fusion - Hectic Vs Hecttech, My New Boyfriend - Carly Simon - Spoiled Girl, Lonely Wanderer - Eulalia - The Basement Demos (File, Album, MP3)