C++中STL的堆栈和序列的用法

来源:互联网 发布:怎样增加淘宝信誉度 编辑:程序博客网 时间:2024/05/22 13:51

使用C++的STL容器可以避免重复造轮子,几年前就想学一下了,拖到现在也是很迷。

其实用法简单的很,这里记录一下关键字。

堆栈的使用:

#include <iostream>#include<stack>using namespace std;int main(){    stack<int> s;    s.push(1);    s.push(2);    while(s.empty()!=true)    {        cout << s.top() << endl ;        cout << "size of stack is " << s.size() << endl ;        s.pop() ;    }    return 0;}

序列的使用:

#include <iostream>  #include <queue>  using namespace std;  //这几个头文件必不可少     int main()  {      queue<int> q;               //使用前需定义一个queue变量,且定义时已经初始化      while(!q.empty()) q.pop();  //重复使用时,用这个初始化      q.push(1);        //进队列      q.pop();          //出队列      int v=q.front();  //对象.成员 得到队首的值      int s=q.size();   //对象.成员函数 得到队列里元素个数      return 0;  }  


0 0
原创粉丝点击