What is the difference between sequence containers and container adaptors in c++?
Here i mean sequence containers as vectors, deque, list while container adaptors as stack, queue, priority_queue. When do we prefer sequence containers and container adaptors?
The container adaptors use containment to provide limited access to sequence container functionality. Many of the container adaptor public methods are simply wrappers around calls to this non-public element. If your application can live with the limited functionality of a container adaptor it is better to use the container adaptor.
Suppose you just used one of the sequence containers directly to implement a queue. You call push_front
to add to the queue, pop_back
to remove from it. Now along comes some bozo maintainer and calls pop_front
instead of pop_back
. If you don't want somebody to pop the wrong end of the thing that you intend to be used as a queue or stack, don't provide that functionality. The container adaptors intentionally do not provide full access to the underlying sequence container.
On the other hand, if you need to get under the hood (for example, you need to peek at the second element in a stack), you are going to need to use a sequence container instead of an adaptor. You might want to think of using the adaptor philosophy: Don't export the full functionality of the container. Just export the functionality that truly is needed.
A sequence is a particular kind of container, in which the elements have an order independent of their values. Instead, it's the order they're appended to the container (or inserted, etc).
A container adaptor is not a container at all, since it doesn't implement the interface of a container (doesn't provide an iterator, etc). Instead, it provides limited access to a container. Those provided in the standard library operate using a sequence.
You use a container adaptor when you want limited access to a collection of objects. Both stack and queue only let you add at one end. In addition, stack only lets you read/remove at the same end you write to, and queue only lets you read/remove at the opposite end. They don't do anything that a sequence doesn't already do, except for stopping you iterating over all elements and so on.
priority_queue's behavior is a bit more complicated, and it does add behavior that isn't already part of the sequence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With