怎么看网站是哪个公司做的,做网站资料准备什么,广东新闻发布会,网站建设几个要素目录
stack的定义
stack容器内元素的访问
stack常用函数实例解析
stack的常见用途 stack的定义
其定义的写法和其他STL容器相同#xff0c;typename可以任意基本类型或容器#xff1a;
stacktypename name;
stack容器内元素的访问
由于栈本身就是一种后进先出…目录
stack的定义
stack容器内元素的访问
stack常用函数实例解析
stack的常见用途 stack的定义
其定义的写法和其他STL容器相同typename可以任意基本类型或容器
stacktypename name;
stack容器内元素的访问
由于栈本身就是一种后进先出的数据结构在STL的stack中只能通过top()来访问栈顶元素。
#includeiostream
#includestack
using namespace std;
int main(){stackint st;for(int i1;i5;i){st.push(i);}printf(%d\n,st.top());return 0;
}
stack常用函数实例解析
(1)push()
push(x)将x入栈时间复杂度为
(2)top()
top()获得栈顶元素时间复杂度为
(3)pop()
pop()用以弹出栈顶元素时间复杂度为
#includestdio.h
#includestack
using namespace std;
int main(){stackint st;for(int i1;i5;i){st.push(i);}for(int i1;i3;i){st.pop();}printf(%d\n,st.top());return 0;
}
(4)empty()
empty()可以检测stack内是否为空返回true为空返回false为非空时间复杂度为
#includeiostream
#includestack
using namespace std;
int main(){stackint st;if(st.empty()true){printf(Empty\n);}else{printf(Not Empty\n);}st.push(1);if(st.empty()true){printf(Empty\n);}else{printf(Not Empty\n);}return 0;
}
(5)size()
size()返回stack内元素的个数时间复杂度为
#includestdio.h
#includestack
using namespace std;
int main(){stackint st;for(int i1;i5;i){st.push(i);}printf(%d\n,st.size());return 0;
}
stack的常见用途
stack用来模拟实现一些递归防止程序对栈内存的限制而导致程序运行出错。