首页 > 编程语言 > 详细

C++复习栈和队列

时间:2020-02-05 12:10:24      阅读:67      评论:0      收藏:0      [点我收藏+]

栈stack


 

  • 先进后出
  • 头文件<stack>
  • stack<int>s;
  • 基本操作
  • 入栈——s.push(a);
  • 出栈——s.pop();
  • 返回大小——s.size();
  • 是否为空——s.empty();
  • 返回栈顶——s.top();

 

队列queue


  • 先进先出

  • 头文件<queue>
  • queue<int>q;
  • 基本操作
  • 入队——q.push(a);
  • 出队——q.pop();
  • 返回大小——q.size();
  • 是否为空——q.empty();
  • 返回队首——q.front();
  • 返回队尾——q.back();

优先队列


 

  • 内部有序 
  • 头文件<queue>
  • priority_queue<int>q;
  • 基本操作
  • q.push(a);
  • q.pop();
  • q.top();
  • q.size();
  • q.empty();
  • 使用
  • #include<queue>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct cmp{
        operator ()(string &a,string &b){
            if(a[0] != b[0])return a[0] < b[0];
            else return a > b;
        }
    };   //运算符重载 
    struct student{
        int grade;
        string name;
        friend bool operator < (student a, student b){
            return a.grade > b.grade; 
        }
    };
    int main(){
        priority_queue<int>q; //大的优先
        priority_queue<int,vector<int>,greater<int> >q;  //小的优先
        //自定义优先级
        priority_queue<string,vector<string>,cmp>q;
        //结构体 
        student s[4] = {
            {67,"Kite"},
            {59,"Tom"},
            {100,"Jim"},
            {98,"Mark"}
        };
        priority_queue<student>q;
        return 0;
    }


 

C++复习栈和队列

原文:https://www.cnblogs.com/Cmathe/p/12262719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!