zoukankan      html  css  js  c++  java
  • 重载运算符-operator

    先看段代码:

    struct node
    {
        friend bool operator< (node n1, node n2){   // 优先取最小的,它与下面的 // 是等价的
            return n1.e > n2.e;
        }
       //bool operator< (const node &r)const{ // <是重载运算符,不是普通的小于
        //    return s > r.s;
        //} int s, e; node(int a, int b){ s = a; e = b; } };
    //bool operator< (const node& a, const node& b){ // 其也是等价的
    //    return a.s > b.s;
    //} int main() { priority_queue<node>que; // 此优先队列实现的是取最小的 que.push(node(1, 10)); que.push(node(5, 50)); que.push(node(2, 20)); while(!que.empty()){ printf("%d ", que.top().s); que.pop(); } return 0; }  

     在结构体中

    struct node
    {
        int s, e;
        node(int _v , int _c ):s(_v),e(_c){} // 这样写是可以进行附初值
                                            // 同理与下面的 // 是等价的 
        //node(int a, int b){
            //s = a;
            //e = b;
        //}
    };
    
    int main() {
        queue<node>que;
        
        que.push(node(1, 10));
        que.push(node(5, 50));
        que.push(node(2, 20));
        //while(!que.empty()){
            //printf("%d
    ", que.top().s);
            //que.pop();
        //}
        while(!que.empty()){
            printf("%d %d
    ", que.front().s, que.front().e);
            que.pop();
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    H5实现查看图片和删除图片的效果
    HTTP 状态码(HTTP Status Code)
    HashMap详解(基于JDK 1.8)
    FutureTask详解
    ForkJoin框架详解
    final详解
    磁盘管理之inode与block
    如何在CentOS上安装一个2048小游戏
    Linux帮助手册(man)
    Linux入门-基础命令
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7774725.html
Copyright © 2011-2022 走看看