Abstracting with std::stack<T> and std::queue<T>
We've now seen three different standard containers with the member functions push_back()
and pop_back()
(and, although we didn't mention it, back()
to retrieve a reference to the last element of the container). These are the operations we'd need if we wanted to implement a stack data structure.
The standard library provides a convenient way to abstract the idea of a stack, with the container known as (what else?) std::stack
. Unlike the containers we've seen so far, though, std::stack
takes an extra template parameter.
std::stack<T, Ctr>
represents a stack of elements of type T
, where the underlying storage is managed by an instance of the container type Ctr
. For example, stack<T, vector<T>>
uses a vector to manage its elements; stack<T, list<T>>
uses a list; and so on. The default value for the template parameter Ctr
is actually std::deque<T>
; you may recall that deque
takes up more memory than...