Defining an Adaptor
Each adaptor defines two constructors: the default constructor that creates an empty object, and a constructor that takes a container and initializes the adaptor by copying the given container. For example, assuming that deq
is a deque<int>
, we can use deq
to initialize a new stack
as follows:
stack<int> stk(deq); // copies elements from deq into stk
By default both stack
and queue
are implemented in terms of deque
, and a priority_queue
is implemented on a vector
. We can override the default container type by naming a sequential container as a second type argument when we create the adaptor:
// empty stack implemented on top of vectorstack<string, vector<string>> str_stk;// str_stk2 is implemented on top of vector and initially ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.