一、stack 容器
1、stack 容器基本概念
概念:stack 是一种先进后出的数据结构,只有一种接口。
栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为。
2、stack 常用接口
构造函数:
-
stack stk:采用模版类实现,stack对象的默认构造形式
-
stack(const stack &s):拷贝构造函数
赋值操作:
- stack &operator=(const stack &stk) :重载等号操作符
数据存取:
-
push(elem):添加元素
-
pop():从栈顶移出第一个元素
-
top():返回栈顶元素
大小操作:
-
empty():判断栈是否为空
-
size():返回栈的大小
void te01(){
int a = 0;
stack < int > s;
s.push(10);
s.push(435);
s.push(2);
s.push(62);
s.push(18);
s.push(88);
stack <int> s1;
s1 = s;
cout << s.size() << endl;
while(!empty(s)){
cout << s.top() << " ";
s.pop();
}
cout << endl;
while(!empty(s1)){
cout << s1.top() << " ";
s1.pop();
}
cout << endl;
}
二、queue 容器
1、queue 基本概念
queue 是一种先进先出的数据接口
2、queue 常用接口
构造函数:
-
queue que :queue 对象的默认构造形式
-
queue (const queue &que) :拷贝构造函数
赋值操作:
- queue &operator = (const queue &que):重载等号操作
数据存取:
-
push(elem):队尾添加元素
-
pop() :对头移出第一个元素
-
back():返回最后一个元素
-
front():返回第一个元素
大小操作:
-
size():返回栈的大小
-
3:判断栈是否为空
void te02(){
queue<int> q;
q.push(10);
q.push(76);
q.push(43);
q.push(72);
q.push(24);
q.push(45);
cout << q.size() << endl;
cout << q.front() << endl;
cout << q.back() << endl;
while(!empty(q)){
cout << q.front() << " ";
q.pop();
}
cout << endl;
cout << q.size() << endl;
}