P
pauldepstein
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Paul Epstein
container works better than vector or deque?
Paul Epstein
pauldepstein said:Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Thomas said:Of course, the only way to know for sure would be to profile your code
and see whether it made a difference.
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Is this for a quiz? Use list when random inserts and deletes must take
constant (minimal) time.
Phlip said:Is this for a quiz?
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Paul Epstein
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Paul Epstein
Consider a waiting list for liver transplant patients. A queue sounds like
a good first choice: first in, first out. But consider this:
Suppose you need a liver transplant. You may get placed on the list
according to how soon you're likely to need the transplant, not just at the
end because you're the latest to apply. That requires inserting you in a
specific order with in the list, something a list is good for, because it
doesn't have to shuffle everyone behind you backwards in the line. You just
get inserted in the appropriate place.
Suppose someone else waiting for a transplant dies before getting a
transplant. They need to be removed from the list. Again, that's easy with
a list.
Neither operation is easy (or at least efficient) with a vector or deque.
You might also want to merge your waiting list with one from another region.
The list has a merge operation for that. Or ,you may want to re-sort the
list, according to new regulations regarding patient priority. Again, list
provides that ability.
Basically, any situation which would normally call for a doubly-linked list,
the list container is likely to fit the bill best. (Unless you've got a
whiz-bang third-party container which is suited even better, of course!)
Yes, but what situation does call for a doubly-linked list? So far, I've
seen few or none, and based on your example, I have to guess you haven't
either.
Jerry Coffin said:Under the circumstances, what you probably want is map, with the
priority as the key and a pointer to the patient's data as the
associated data.
There is also std:riority_queue which may have been a better choice
for this example.
Jerry Coffin wrote:
...
I have used list<>s for:
a) work queues - i.e. items of work that may be removed before being done.
b) message queues - i.e. a list of messages that need to be diced and
spliced.
c) client objects - i.e. each client of a service is an item in a list
Can anyone give a realistic example of a task for which the list
container works better than vector or deque?
Gianni said:One important property for list is that an iterator to a list element
remains valid for all insertion/deletion operations except for deleting
the element the iterator is pointing to. For a deque or a vector, any
insertions or deletions can invalidate all iterators.
Jens said:Mergesort can be implemented in-situ on lists, but not on deques and
vectors.
Not sure if this counts as a realistic example though.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.