堆栈是一个类型的容器的适配器,专门设计在一个LIFO上下文(后进先出),其中元件被插入并只从容器的一端萃取操作。
栈实现为容器适配器,其是使用特定容器类的封装对象作为其底层的容器类,提供一组特定的成员函数来访问它的元素。元件推/从特定的容器,这是被称为堆顶部的“背面”弹出。
底层容器可以是任何标准的容器类模板或一些其它专门设计的容器类。容器应支持以下操作:
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/156bba0f-8e4b-4f03-9ae5-cdf09227f5ca.htm
empty 判断是否为空
size 返回堆栈元素的数量
top 返回位于堆栈顶部的引用的元素
push 将一个元素添加到堆栈的顶部
pop 从栈中删除顶部的元素
#include <stack>
#include <vector>
#include <list>
#include <iostream>
//构造函数
void StackConstructor(void);
//判断是否为空
void Stack_empty(void);
//从栈中删除顶部的元素
void Stack_pop(void);
//将一个元素添加到堆栈的顶部
void Stack_push(void);
//返回堆栈元素的数量
void Stack_size(void);
//返回位于堆栈顶部的引用的元素
void Stack_top(void);
int main()
{
//StackConstructor();
//Stack_empty();
//Stack_pop();
//Stack_push();
//Stack_size();
Stack_top();
return 0;
}
//构造函数
void StackConstructor(void)
{
using namespace std;
// Declares stack with default deque base container
stack <char> dsc1;
//Explicitly declares a stack with deque base container
stack <char, deque<char> > dsc2;
// Declares a stack with vector base containers
stack <int, vector<int> > vsi1;
// Declares a stack with list base container
stack <int, list<int> > lsi;
// The second member function copies elements from a container
vector<int> v1;
v1.push_back(1);
stack <int, vector<int> > vsi2(v1);
cout << "The element at the top of stack vsi2 is "
<< vsi2.top() << "." << endl;
/*
The element at the top of stack vsi2 is 1.
请按任意键继续. . .
*/
return;
}
//判断是否为空
void Stack_empty(void)
{
using namespace std;
// Declares stacks with default deque base container
stack <int> s1, s2;
s1.push(1);
if (s1.empty())
cout << "The stack s1 is empty." << endl;
else
cout << "The stack s1 is not empty." << endl;
if (s2.empty())
cout << "The stack s2 is empty." << endl;
else
cout << "The stack s2 is not empty." << endl;
return;
/*
The stack s1 is not empty.
The stack s2 is empty.
请按任意键继续. . .
*/
}
//从栈中删除顶部的元素
void Stack_pop(void)
{
using namespace std;
stack <int> s1, s2;
s1.push(10);
s1.push(20);
s1.push(30);
stack <int>::size_type i;
i = s1.size();
cout << "The stack length is " << i << "." << endl;
i = s1.top();
cout << "The element at the top of the stack is "
<< i << "." << endl;
s1.pop();
i = s1.size();
cout << "After a pop, the stack length is "
<< i << "." << endl;
i = s1.top();
cout << "After a pop, the element at the top of the stack is "
<< i << "." << endl;
return;
/*
The stack length is 3.
The element at the top of the stack is 30.
After a pop, the stack length is 2.
After a pop, the element at the top of the stack is 20.
请按任意键继续. . .
*/
}
//将一个元素添加到堆栈的顶部
void Stack_push(void)
{
using namespace std;
stack <int> s1;
s1.push(10);
s1.push(20);
s1.push(30);
stack <int>::size_type i;
i = s1.size();
cout << "The stack length is " << i << "." << endl;
i = s1.top();
cout << "The element at the top of the stack is "
<< i << "." << endl;
return;
/*
The stack length is 3.
The element at the top of the stack is 30.
请按任意键继续. . .
*/
}
//返回堆栈元素的数量
void Stack_size(void)
{
using namespace std;
stack <int> s1, s2;
stack <int>::size_type i;
s1.push(1);
i = s1.size();
cout << "The stack length is " << i << "." << endl;
s1.push(2);
i = s1.size();
cout << "The stack length is now " << i << "." << endl;
return;
/*
The stack length is 1.
The stack length is now 2.
请按任意键继续. . .
*/
}
//返回位于堆栈顶部的引用的元素
void Stack_top(void)
{
using namespace std;
stack <int> s1;
s1.push(1);
s1.push(2);
int& i = s1.top();
const int& ii = s1.top();
cout << "The top integer of the stack s1 is "
<< i << "." << endl;
i--;
cout << "The next integer down is " << ii << "." << endl;
return;
/*
The top integer of the stack s1 is 2.
The next integer down is 1.
请按任意键继续. . .
*/
}