参见http://www.cplusplus.com/reference/stack/stack/
template<class T, class Container = deque<T>> class stack;
LIFO stack
Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.
[堆栈(stack)是一种容器适配器,具有后进先出的结构元素的插入和提取都只在容器的一端进行]
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. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.
[容器适配器是一个类,这个类使用一个特定的容器类对象作为它的内在容器,该内在容器提供了一系列的成员函数来读取它的元素]
The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall support the following operations:
[堆栈的内在容器可以是一个标准容器类模板或者其他专门设计的容器类,但无论如何,内在容器都应该支持以下几种操作:]
empty
size
back
push_back
pop_back
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.
[标准容器类vector、deque和list都满足这些要求。默认的内在容器是deque]
/* //construct stack stack (const container_type& ctnr = container_type()); ctnr Container object. [ctnr是一个容器类对象] container_type is the type of the underlying container type (defined as an alias of the second class template parameter, Container; see member types). [ctnr的数据类型container_type是内在容器的数据类型,即第二个模板参数Container] A container adaptor keeps internally a container object as data. This container object is a copy of the ctnr argument passed to the constructor, if any, otherwise it is an empty container. [stack会将一个容器对象作为数据,这个容器对象是传递到构造函数中的参数ctnr,如果没有传递参数ctnr则为空容器] */ #include <iostream> #include <stack> #include <vector> #include <deque> int main() { std::deque<int> mydeque(3, 100); std::vector<int> myvector(2, 200); std::stack<int> first; // empty stack with deque as underlying container std::stack<int> second(mydeque); // stack initialized to copy of deque with deque as underlying container std::stack<int, std::vector<int>> third; //empty stack with vector as underlying container std::stack<int, std::vector<int>> fourth(myvector); //statck initialized to copy of vector with vector as underlying container std::cout << "size of first: " << first.size() << ‘\n‘; std::cout << "size of second: " << second.size() << ‘\n‘; std::cout << "size of third: " << third.size() << ‘\n‘; std::cout << "size of fourth: " << fourth.size() << ‘\n‘; system("pause"); return 0; }
/* bool empty() const; size_type size() const; void push(const value_type& val); void pop(); value_type& top(); */ #include <iostream> #include <stack> int main() { std::stack<int> mystack; for(int i=0; i<5; i++) mystack.push(i*10); std::cout<<"mystack containes: "; while(!mystack.empty()) { std::cout<<mystack.top()<<‘ ‘; mystack.pop(); } std::cout<<‘\n‘; system("pause"); return 0; }