While reading “Effective C++” by Scott Meyers, the author says with reference to this particular book, “the STL” means part of C++ STL that works with iterators. But it excludes anything that does not support iterators (like stack, queue, and priority_queue). That got me thinking whats difference between container and container adapter class. Why the distinction.
STL Container:
A STL Container is a template class that directly stores elements and manages their organisation in memory. It also provides full iterator support and support many algorithms.
Common STL Containers:
Examples:
std::vector
– Dynamic arraystd::list
– Doubly linked liststd::deque
– Double-ended queuestd::set
,std::map
, etc. – Tree-based associative containersstd::unordered_map
,std::unordered_set
– Hash-based containers
STL Container Adapter:
A container adapter is a wrapper around an existing container that restricts or modifies its interface to behave like a specific data structure.
Characteristics:
Uses another container internally (like
deque
orvector
).Hides the underlying container's full functionality.
Provides a simplified or specialized interface (like stack behavior).
Examples:
std::stack
– LIFO stack (typically backed bydeque
)std::queue
– FIFO queue (also typically usesdeque
)std::priority_queue
– Heap-based priority queue
Basically, a container adapater is a wrapper that uses a container under the hood.