简体   繁体   中英

Basic questions about std::list

I want to use a list to hold a sequence of objects which hold information about tasks I will perform. Once one of these objects is finished, I no longer need it; I just need to go to the next object in the list. It's basically a queue.

Regarding pointers to these objects:

Once I get a pointer to a list object to act upon, if I remove other elements in the list, this pointer remains valid, right? Because once I finish with my first object, I will move onto the next and I will pop that first object while I continue to work with the next object, that now becomes the first object. Essentially I will always be working on the first object in the list. I want to make sure pointers to existing list items are not invalidated even if I remove other list items.

(Incidentally, this is the main reason I want to use a list , so I can manipulate it without requiring all other list objects to potentially be moved to a new space in memory, as is the case with vector which would make this expensive)

Regarding memory:

When I add something to the list, can I expect the list to take ownership and if my original item goes out of scope while the list remains in member variable scope, my object will still persist? And then, once it is popped, the item object will finally go out of scope?

Once I get a pointer to a list object to act upon, if I remove other elements in the list, this pointer remains valid, right?

Yes, because pointer to std::list is not the pointer to the head element of the plain double-linked list - std::list class has internal pointers and it keeps them away from the user (you can iterate list using iterators, but not using plain pointers).

When I add something to the list, can I expect the list to take ownership and if my original item goes out of scope while the list remains in member variable scope, my object will still persist? And then, once it is popped, the item object will finally go out of scope?

You need to create std::list<T> and not std::list<T*> . This way, when you add element to the list, copy-constructor will be called, and copy of the object will be placed in the list, and not the actual object.

if I remove other elements in the list, this pointer remains valid, right?

Yes, Iterators to list elements remain valid unless you explicitly remove the element from the list or clear the list.

When I add something to the list, can I expect the list to take ownership and if my original item goes out of scope while the list remains in member variable scope, my object will still persist?

Yes, kind of. The list copies the elements you push into it. It doesn't really take ownership, rather it creates a new object, which it owns.

And then, once it is popped, the item object will finally go out of scope?

Yes, the object will cease to exist.

Note that all bets are off if you pass pointers to objects. In this case, the list only controls the lifetime of the pointers it holds, not the objects these point to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM