std::stack 如何组织内部存储?
How std::stack organizes internal storage?
如何std::stack在幕后组织存储,在什么样的结构中分配它的存储?它就像一个矢量延续记忆吗?或者它就像列表?
stack 是容器适配器。它可以在任何容器上工作,模板参数中将指向它,默认为 std::deque
.
它使用作为第二个模板参数传递的数据结构,默认情况下 std::deque
。
您可以传递满足以下要求的任何容器 class:
The type of the underlying container to use to store the elements. The container must satisfy the requirements of SequenceContainer. Additionally, it must provide the following functions with the usual semantics:
- back()
- push_back()
- pop_back()
The standard containers std::vector
, std::deque
and std::list satisfy
these requirements.
documentation显然有答案:
stacks are implemented as containers adaptors, which are classes that
use an encapsulated object of a specific container class as its
underlying container, providing a specific set of member functions to
access its elements.
.........................
The standard container classes vector, deque and list fulfill these
requirements. By default, if no container class is specified for a
particular stack class instantiation, the standard container deque is
used.
如何std::stack在幕后组织存储,在什么样的结构中分配它的存储?它就像一个矢量延续记忆吗?或者它就像列表?
stack 是容器适配器。它可以在任何容器上工作,模板参数中将指向它,默认为 std::deque
.
它使用作为第二个模板参数传递的数据结构,默认情况下 std::deque
。
您可以传递满足以下要求的任何容器 class:
The type of the underlying container to use to store the elements. The container must satisfy the requirements of SequenceContainer. Additionally, it must provide the following functions with the usual semantics:
- back()
- push_back()
- pop_back()
The standard containers
std::vector
,std::deque
andstd::list satisfy
these requirements.
documentation显然有答案:
stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements.
.........................
The standard container classes vector, deque and list fulfill these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.