iis网站发布默认首页,图片编辑工具免费版,有哪些静态网站,wordpress自动保存外链mp3#xff08;一#xff09;stack容器的入栈与出栈
#xff08;1#xff09;stack容器的简介
stack堆栈容器#xff0c;“先进后出”的容器#xff0c;且stack没有迭代器
#xff08;2#xff09;stack对象的默认构造 stack采用模板类实现#xff0c;stack对象的默认…一stack容器的入栈与出栈
1stack容器的简介
stack堆栈容器“先进后出”的容器且stack没有迭代器
2stack对象的默认构造 stack采用模板类实现stack对象的默认构造形式stack T s; 1.stack int stklnt;//一个存放int的stack容器 2.stack float stkFloat;//ー个存放float的stack容器 3.stack stringstkString;//一个存放string的stack容器
stack的push()与pop()方法
stack.push(elem);//栈头添加元素stack.pop()//栈头移除第一个元素
#include iostream #include stack using namespace std;
int main() { stackint stk; stk.push(1); stk.push(2); stk.push(3); stk.push(4); stk.push(5); cout stk.top() endl; //top函数返回栈顶元素不会删除栈顶元素 stk.pop(); //出栈删除栈顶元素 cout stk.top() endl; stk.pop(); return 0; } #include iostream #include stack using namespace std;
int main() { stackint stk; stk.push(1); stk.push(2); stk.push(3); stk.push(4); stk.push(5); while(stk.empty() !true) { coutstk.top() endl; stk.pop(); } return 0; } 二)stack容器的拷贝构造方法
1.stack(const stack stk);//拷贝构造函数
#include iostream #include stack using namespace std;
int main() { stackint stk; stk.push(1); stk.push(2); stk.push(3); stk.push(4); stk.push(5); stackint stk1(stk); while(stk1.empty() !true) { coutstk1.top()endl; stk1.pop(); } return 0; } 2.stackoperator(const stack stk);//重载等号操作符
#include iostream #include stack using namespace std;
int main() { stackint stk; stk.push(1); stk.push(2); stk.push(3); stk.push(4); stk.push(5); stackint stk1(stk); while(stk1.empty() !true) { coutstk1.top()endl; stk1.pop(); } stackintstk2stk; stk2stk; return 0; } 三stack的大小
stack.empty();//判断堆栈是否为空
stack.size();//返回堆栈的大小
#include iostream #include stack using namespace std;
int main() { stackint stk; stk.push(1); stk.push(2); stk.push(3); stk.push(4); stk.push(5); coutstk sizestk.size()endl; return 0; }