• c++的优先队列的比较函数与Java的比较函数


    package com.file;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import java.util.Arrays;
    
    
    class Solution{
        
        private FileInputStream fin;
        private FileOutputStream fout;
        private byte data[];
        
        public boolean openFile() throws IOException {
            fin=new FileInputStream("F:\tdm-gcc.zip");
            fout=new FileOutputStream("F:\1.zip");
            data=new byte[1024];
            while(fin.read(data)!=-1) {
                fout.write(data);
                data=new byte[1024];
            }
            fin.close();
            fout.close();
            return true;
        }
    }
    
    ------------------------------------自定义比较函数----------------------------------------------------------------------
    class student implements Comparable<student>{ private String name; private int age; public student(String name, int age) { super(); this.name = name; this.age = age; } @Override public int compareTo(student o) { if(this.age < o.age) return 1; if(this.age > o.age) return -1; return 0; } @Override public String toString() { return "name: "+this.name+","+"age: "+this.age; } } class SortStudent{ public boolean Sort() { student stu=new student("***",14); student stu1=new student("(((",16); student stu2=new student(")))",12); student[]arr= {stu,stu1,stu2}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); return true; } } public class Main { public static void main(String[] args) throws IOException { SortStudent ss=new SortStudent(); ss.Sort(); } }

    -----------------------------------以一个结构体作为节点的优先队列-------------------------------------------------------------------------------- #include
    <iostream> #include <vector> #include <algorithm> #include <string> #include <queue> #include <functional> using namespace std; class student{ private: string name; int age; public: bool operator<(const student& obj)const{ return this->age > obj.age; } student(string name, int age){ this->name = name; this->age = age; } string GetName(){ return this->name; } }; int main(){ student s1("****",12); student s2("(((", 15); student s3(")))", 14); priority_queue<student>pq; pq.push(s1); pq.push(s2); pq.push(s3); cout << pq.top().GetName() << endl; pq.pop(); cout << pq.top().GetName() << endl; pq.pop(); cout << pq.top().GetName() << endl; pq.pop(); cin.get(); }


    //---------------------------------补充一点在类外的比较函数的定义-------------------------------------------
    #include <iostream>
    #include <queue>
    #include <functional>
    #include <vector>
    using namespace std;

    struct cmp
    {
        bool operator()(vector<int>a,vector<int>b)
        {
            if(a[0]>b[0])return true;          //大于则交换------>小顶堆
            else if(a[0]==b[0])return a[1]>b[1];
        }
    };

    class Solution {
    public:
        vector<vector<int>> merge(vector<vector<int>>& intervals) {

                    priority_queue<vector<int>,vector<vector<int>>,cmp>queue;
                    for(int i=0;i<intervals.size();++i)
                    {
                        queue.push(intervals[i]);
                    }
                    for(int i=0;i<intervals.size();++i)
                    {
                        vector<int>temp=queue.top();
                        cout<<temp[0]<<":"<<temp[1]<<endl;
                        queue.pop();
                    }
                    return intervals;
        }
    };

    int main()
    {
       Solution space;
       vector<vector<int>>intervals;
       vector<int>a;
       vector<int>b;
       a.push_back(5);
       a.push_back(7);
       b.push_back(2);
       b.push_back(3);
       intervals.push_back(a);
       intervals.push_back(b);
       space.merge(intervals);
       cout<<"sss"<<endl;
        return 0;
    }
  • 相关阅读:
    Java基础知识【上】(转载)
    Windows下Python中pip安装Pillow报错总结(转载)
    CentOS7下安装Python的pip
    CentOS7安装NodeJS6.9
    PostGIS(解压版)安装
    CentOS7中安装Python3.5
    CentOS7安装docker
    Centos7更改默认启动模式(转载)
    CentOS7 查看IP、Gateway、DNS、Hostname
    CentOS7系统安装及初始化
  • 原文地址:https://www.cnblogs.com/z2529827226/p/11621860.html
Copyright © 2020-2023  润新知