stack与queue

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。stack与queue,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

stack

stack模板类的定义在<stack>的头文件中;

定义stack对象的范例如下;

stack<int> s;
stack<double> s;
stack的基本操作:
s.top() ;     //栈顶
s.push(a) ;  //元素a入栈
s.pop();   //栈顶元素出栈
s.empty(); //判断栈空,如果为空,则返回true
s.size();    //返回栈中元素个数;

代码示例:

#include <iostream>
#include <stack>
using namespace std;
stack<int> s;

int main(){
	int n ;
	cin>>n;
	for(int i = 0 ; i < n ; i++) s.push(i+1);
	cout<<s.size()<<endl;
	while(!s.empty()){
		cout<<s.top()<<"\t";
		s.pop();
	}
	cout<<endl;
	system("pause");
	return 0;
}

queue

queue模板类定义在<queue>的头文件中;

定义queue对象的范例如下:

queue<int> q;
queue<double> q;
queue的基本操作:
q.front();     //队首
q.back();    //队尾
q.push(a);   //元素a入队
q.pop();   //出队
q.empty(); //判断队列空,如果为空,返回true
q.size();    //返回队中元素个数
#include <iostream>
#include <queue>
using namespace std;
queue<int> q;

int main(){
	int n ;
	cin>>n;
	for(int i = 0 ; i < n ; i++)  q.push(i+1);
        cout<<q.size()<<endl;
	while(!q.empty()){
		cout<<q.front()<<"\t";
		q.pop();
	}
	cout<<endl;
	system("pause");
	return 0;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/163064.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!