zoukankan      html  css  js  c++  java
  • 堆STL和重载运算符

    • 大根堆:

        1.priority_queue<int> q;【默认

        2. priority_queue< node,vector<node>,less<node> > q;【自带比较函数

    • 小根堆:

        priority_queue< node,vector<node>,greater<node> > q;【自带比较函数

    • 重载运算符:

       1.定义struct node 之后重载

        这是小根堆

    1 priority_queue<node>q;//或 priority_queue< node,vector<node>,less<node> >q; 
    2 bool operator<(node a,node b)//或写做(const node &a,const, node &b) 更快 
    3 {
    4     return a.y>b.y;//以y从小到大排序
    5 }

       2.在定义struct node时重载

    1 struct node
    2 {
    3     int id,v;
    4     bool operator<(const node &a) const{
    5         return v<a.v;//以v从大到小排序
    6     }
    7 };
    8 priority_queue<node>q;

       3.定义友元操作类重载函数

    1 struct node
    2 {
    3     int v;
    4     friend bool operator<(const node &a,const node &b){
    5         return a.v<b.v;  //按v从大到小排列
    6     }
    7 };
    8 priority_queue<node>q;
    • 自定义比较函数模板结构
    1 struct cmp
    2 {
    3     bool operator ()(const node &a, const node &b)
    4     {
    5         return a.v>b.v;// 按照v从小到大排列
    6     }
    7 };
    8 priority_queue<node,vector<node>,cmp> q;
  • 相关阅读:
    P2788 数学1(math1)- 加减算式
    数据库第三章-学习笔记
    字典序
    P1739 表达式括号匹配
    P3742 umi的函数
    P1765 手机
    P2192 HXY玩卡片
    全排函数c++ next_permutation()
    11.css定义下拉菜单
    10.php引用(&)详解及注意事项
  • 原文地址:https://www.cnblogs.com/kylara/p/9804251.html
Copyright © 2011-2022 走看看