stack与queue

stack

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

定义stack对象的范例如下;

stack<int> s;

stack<double> s;

stack的基本操作:

s.top() ;     //栈顶

s.push(a) ;  //元素a入栈

s.pop();   //栈顶元素出栈

s.empty(); //判断栈空,如果为空,则返回true

s.size();    //返回栈中元素个数;

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4 stack<int> s;
 5 
 6 int main()
 7 {
 8     int n ;
 9     cin>>n;
10     for(int i = 0 ; i < n ; i++)
11         s.push(i+1);
12     cout<<"s.size():"<<s.size()<<endl;
13     while(!s.empty())
14     {
15         cout<<s.top()<<"\t";
16         s.pop();
17     }
18     cout<<endl;
19     system("pause");
20     return 0;
21 }
stack

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(): "<<q.size()<<endl;
    while(!q.empty())
    {
        cout<<q.front()<<"\t";
        q.pop();
    }
    cout<<endl;
    system("pause");
    return 0;
}
queue

 

转载于:https://www.cnblogs.com/satan-shanks/p/3697263.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值