I'm new to the C++ programming world and am having a bit of difficulty understanding the difference between a container and container adapter. I thought a container such as a vector, deque and list that hold objects.
And an adapter is defined as something that converts something else such as deque being changed into a queue.
This is where I am missing the big light bulb moment - isn't a queue just another container with its own set of member functions that operates on whatever is inside the queue?


A queue is a very basic concept which requires only a few functions. The idea behind a queue is just to be able to add elements to the back (push), remove elements from the front (pop) and inspect the elements on either end. This is an abstract and functional concept that one can use in an algorithm. However, there are infinitely many ways to implement that basic concept, and in most cases, the way the queue is implemented has very little or no impact at all on the algorithm that uses it. And thus, you would normally write the algorithm in a generic way that can handle any implementation of a queue. But, because different implementations of the queue concept might not be able to supply the same set of functions that most STL containers provide (deque, list or vector), you don't want to link the queue concept to a container like deque directly. Instead, you say that a class implementing the queue concept should have four functions (front(), back(), push(), and pop()) which makes that class a "queue", anything more is irrelevant. This way, just about any implementation of the concept of a queue will be able to comply to this concept, and you can write generic algorithms that only rely on these four functions and you will be able to swap out the type of queue you use for any other type, without any trouble. So, this is why the "queue" concept exists, independently of the deque container (which is a prime candidate as the underlying storage). The same goes for the concept of a stack.

STL containers, on the other hand, are much more complicated and involve a lot more strict specifications as to how they are implemented (copying behavior, reallocations, random-access, etc.). And consequently, the standard can define a much richer set of functions that they are required to have, and so, they become swiss-army knives in some sense. The point of the adaptor is to simply make a container look like a queue or stack. Adaptors generally just remap the interface from one interface to another, in this case, it maps the very complex interface of a container to the very minimalistic interface of the "queue" concept.

In other words, a container is a tool for the programmer to store objects and manipulate that set of objects easily, and with lots of functionality. While concepts, like queue and stack, are a minimalistic (minimal requirement) abstraction of a basic concept that one can use in a generic algorithm. In theory, a container could and might have the four functions required by the queue concept directly in its original interface, but the standard committee people judged that it was best to keep them separate, via a container adaptors called std::queue and std::stack.

So, the distinction is mostly conceptual, but has practical implications too.

Thanks so much for the great description and distinction. You made it very clear to see how it works. :)
Thanks again!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.