【2019暑假刷题笔记-STL绪论(二)】总结自《算法笔记》

本文详细介绍了C++标准模板库(STL)中的三种重要容器:queue、priority_queue和stack的常见用法,以及algorithm头文件下的常用函数。包括各种函数的使用方法,如push、pop、front、back、top、empty、size,还有max、min、abs、swap、reverse、next_permutation、fill和sort等,并提供了实例说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

五、queue的常见用法

六、priority_queue的常见用法

七、stack的常见用法

八、algorithm头文件下的常用函数


五、queue的常见用法

queue也就是队列,是STL中实现先进先出的容器。queue是在当需要实现BFS的时候,可以不用自己去手动实现一个队列,而是用queue作为代替,提高代码的准确性。另外要注意的一点是,在使用front()和back()函数之前,需要判断queue是否为空。

  1. queue的定义:
    /*
    queue的定义在STL中比较常规:添加头文件#include <queue>
    
    并且在头文件下边加上using namespace std;然后就可以使用了。
    
    使用模式是queue<typename> name;
    */
    queue<int> q;
    

     

  2. queue的访问:
    /*
    STL中只能通过front()来访问队首元素,或者通过back()函数来访问队尾元素
    */
    printf("%d %d\n",q.front(),q.back());

     

  3. push()函数:
    /*
    push(x)将x进行入队
    */
    q.push(x);

     

  4. front()和back():
    /*
    front()函数获得队首元素,back()函数获得队尾元素
    */
    
    printf("%d %d\n",q.front(),q.back());

     

  5. pop()函数:
    /*
    pop()函数用于让队首元素出队
    */
    
    printf("%d\n",q.pop());

     

  6. empty()函数:
    /*
    empty()函数检查队列是否为空,返回bool类型,true是空,false是非空
    */
    
    if(!q.empty()){
        ……;
    }

     

六、priority_queue的常见用法

priority_queue又称为优先队列,其底层是利用实现的。在priority_queue中,队首元素一定是优先级最高的元素,任何时间向priority_queue中push元素都会随时调整结构,使得每次的队首元素都是优先级最大的。priority_queue可以解决一些贪心问题,也可以对Dijkstra算法进行优化。

  1. priority_queue的定义:
    /*
    priority_queue的定义在STL中比较常规:添加头文件#include <queue>
    
    并且在头文件下边加上using namespace std;然后就可以使用了。
    
    使用模式是priority_queue<typename> name;
    */
    priority_queue<int> q;
    
  2. priority_queue容器内元素的访问:
    /*
    
    priority_queue没有front()函数和back()函数,
    
    只能通过top()函数来访问队首元素,也就是优先级最高的元素
    
    */
    
    printf("%d",q.top());

     

  3. push()函数:
    /*
    push(x)将x进行入队
    */
    q.push(x);
  4. top()函数:
    /*
    
    top()函数获得队首元素
    
    */
    
    printf("%d",q.top());

     

  5. pop()函数:
    /*
    pop()函数就是令队首元素出队的元素,就是优先级最高的元素
    */
    
    q.pop();

     

  6. empty()函数:
    /*
    
    empty()函数检测priority_queue是否为空,
    
    空返回true,非空返回false;
    
    */
    
    if(q.empty()==true)
    
    if(!q.empty())

     

priority_queue内元素优先级的设置:

  1. 基本数据类型 的优先级设置
    /*
    以下两则等价,less<int>表示数字大的优先级大,而greater<int>表示数字小的优先级大
    */
    priority_queue<int> q;
    priority_queue<int,vector<int>,less<int> > q;
    
    /*----------------------------------------------------------*/
    
    priority_queue<int,vector<int>,greater<int> > q;
  2. 结构体优先级设置

    /*举个栗子帮助理解*/
    
    /*
    价格大的优先级大,与sort函数定义相反
    */
    struct fruit{
        string name;
        int price;
        friend bool operator < (fruit f1,fruit f2){
            return f1.price < f2.price;
            //return f1.price > f2.price; 价格大的优先级小
        }
    }
    
    /*-------------------------------------------------------------*/
    /*除了上面一种办法,还有一种办法表示*/
    
    struct fruit{
        string name;
        int price;
    }
    struct cmp{
        bool operator ()(fruit f1,fruit f2){
        return f1.price < f2.price;
        //return f1.price > f2.price; 价格大的优先级小
    }
    
    /*引用*/
    priority_queue<fruit, vector<int>,cmp> q;
    
    /*即使是基本数据类型或者其他STL容器(如set),也可以通过同样的方式来定义优先级*/
    friend bool operator < (const fruit &f1,const fruit &f2){
        return f1.price >f2.price;
    }
    
    bool operator () (const fruit &f1,const fruit &f2){
        return f1.price > f2.price;
    }

     

七、stack的常见用法

栈,后进先出(FILO)

  1. stack的定义:
    /*
    
    定义一个stack需要添加头文件#include <stack>
    
    以及在头文件下方加上using namespace std;
    
    stack<int> st;
    */
    
    stack<int> st;

     

  2. stack容器内的元素访问:
    /*
    
    只能通过top()函数来访问栈顶元素
    
    */
    
    st.top();
  3. push()函数:
    /*
    
    push(x)将x入栈
    
    */
    
    st.push(1);
  4. top()函数:
    /*
    
    top()函数获得栈顶元素
    
    */
    
    st.top();
  5. pop()函数:
    /*
    
    使用pop()函数弹出栈顶元素
    
    */
    
    st.pop();
  6. empty()函数:
    /*
    
    empty()可以检测stack内是否为空,true为空,false为非空
    
    */
    
    if(st.enpty()==true)
  7. size()函数:
    /*
    
    通过size()函数来获得栈的元素个数
    
    */
    
    st.size();

八、algorithm头文件下的常用函数

  1. max()/min()/abs():
    max(x,y);  //返回x,y中的最大值
    min(x,y);  //返回x,y中的最小值
    abs(x);   //返回x的绝对值
    

     

  2. swap():
    swap(x,y); //交换x和y的值
  3. reverse():
    //可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内进行反转
    reverse(it,it2);  
    
    /*数组反转*/
    
    int a[10]={1,2,3,4,5,6,7,8,9,0};
    reverse(a,a+4);
    
    /*容器反转*/
    string str="3843264872";
    reverse(str.begin(),str.begin()+9);
    
  4. next_permutation():
    /*next_permutation()全排列*/
    int a[3]={1,2,3};
    do{
        printf("%d %d %d\n",a[0],a[1],a[2]);
    }while(next_permutation(a,a+3));
    
    /*
    123
    132
    213
    231
    312
    321
    */
    
  5. fill():
    /*
    fill()可以把数组或容器中的某一段区间赋值为某个值
    */
    fill(a,a+4,233);
  6. sort():
    /*
    
    sort(元素首地址(必填),尾地址的下一个地址(必填),比较函数(非必填))
    
    比较函数如果不填,则默认为增序排列
    
    结构体排序
    
    bool cmp(node a,node b){
        return a.x<b.x;
    }
    
    */
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_之桐_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值