目录
stack类介绍
stack类定义
stack类常见构造函数
stack数据操作
empty()函数
size()函数
top()函数
push()函数
pop()函数
swap()函数
template > class stack;
stack类为类模板,所以在使用时需要带上类型表示一个具体的类,例如数据类型为int
类型的stack使用时需要写为stack
构造函数 |
函数原型 |
无参构造函数 |
|
上面表格中的前三个构造函数均含有自定义空间配置器并带有缺省值,目前只使用默认即可
使用stack类需要包含头文件
示例代码:
#include
#include
using namespace std;
int main()
{
stack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
//打印栈
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << endl;
return 0;
}
输出结果:
5 4 3 2 1
函数 |
功能 |
|
判断调用对象栈是否为空栈 |
|
获取调用对象栈中的有效数据个数 |
|
获取调用对象栈中的栈顶元素 |
|
向调用对象栈顶插入元素 |
|
弹出调用对象栈顶元素 |
|
交换调用对象栈和指定栈 |
empty()
函数使用empty()
函数可以判断调用对象栈是否为空栈
函数 |
函数原型 |
|
|
示例代码:
#include
#include
using namespace std;
int main()
{
stack st;
stack st1;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout << "st: " << st.empty() << endl;
cout << "st1: " << st1.empty() << endl;
return 0;
}
输出结果:
st: 0
st1: 1
size()
函数使用size()
函数可以获取调用对象栈中的有效数据个数
函数 |
函数原型 |
|
|
示例代码:
#include
#include
using namespace std;
int main()
{
stack st;
stack st1;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout << "st: " << st.size() << endl;
cout << "st1: " << st1.size() << endl;
return 0;
}
输出结果:
st: 5
st1: 0
top()
函数使用top()
函数可以获取调用对象栈中的栈顶元素
函数 |
函数原型 |
|
|
|
注意,如果栈为空时取栈内元素将会出现断言错误
示例代码:
#include
#include
using namespace std;
int main()
{
stack st;
stack st1;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout << "st: " << st.top() << endl;
// 断言错误
//cout << "st1: " << st1.top() << endl;
return 0;
}
输出结果:
5
push()
函数使用push()
函数可以向调用对象栈内插入数据
函数 |
函数原型 |
|
|
示例代码:
#include
#include
using namespace std;
int main()
{
stack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
//打印栈
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << endl;
return 0;
}
输出结果:
5 4 3 2 1
pop()
函数使用pop()
函数可以弹出调用对象栈的栈顶元素
函数 |
函数原型 |
|
|
注意,当栈中没有元素时,调用pop()
函数会出现断言错误
#include
#include
using namespace std;
int main()
{
stack st;
stack st1;
st.push(1);
st.push(2);
st.push(3);
cout << st.top() << " ";
st.pop();
st.push(4);
st.push(5);
//打印栈
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << endl;
// 断言错误
//cout << "st1: " << st1.top() << endl;
return 0;
}
输出结果:
3 5 4 2 1
swap()
函数使用swap()
函数可以交换调用对象栈和指定对象栈
函数 |
函数原型 |
|
|
示例代码:
#include
#include
using namespace std;
int main()
{
stack st;
stack st1;
st.push(1);
st.push(1);
st.push(1);
st.push(1);
st.push(1);
st1.push(2);
st1.push(2);
st1.push(2);
st1.push(2);
st1.push(2);
cout << "交换前:" << endl;
//打印栈
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << endl;
//打印栈
while (!st1.empty())
{
cout << st1.top() << " ";
st1.pop();
}
cout << endl;
// 注意打印已经使栈为空,需要重新插入元素
st.push(1);
st.push(1);
st.push(1);
st.push(1);
st.push(1);
st1.push(2);
st1.push(2);
st1.push(2);
st1.push(2);
st1.push(2);
st.swap(st1);
cout << "交换后:" << endl;
//打印栈
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << endl;
//打印栈
while (!st1.empty())
{
cout << st1.top() << " ";
st1.pop();
}
cout << endl;
return 0;
}
交换前:
1 1 1 1 1
2 2 2 2 2
交换后:
2 2 2 2 2
1 1 1 1 1