STL 中的 stack 是一种容器适配器,而不是一种容器。

它是容器适配器是指,只要支持一系列方法的容器(empty, size, back, push_back, pop_back),都能作为stack使用。

stack 有可能实际上是一个 vector, deque 或 list. 如果没有特殊指明,将使用 deque作为stack的实际容器。

构造函数

构造函数中包含两个模板参数。stack<stack::alue_type T, stack::container_type Container>

其中 T代表数值类型,可以是基本数据类型或用户自定义类型。

Container代表容器类型,可以是 vector, deque 或 list

1 std::stack<int> first; // empty stack
2 std::stack<int> second(mydeque); // stack initialized to copy of deque
3 std::stack<int, std::vector<int>> third; // empty stack using vector
4 std::stack<int, std::vector<int>> fourth(myVector); // stack initialized to copy of vector using vector
View Code

相关文章: