• 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;
    }
  • 相关阅读:
    小程序前端直传阿里云oss的一些记录
    小程序的两种分页做法(后端返回分页及总页数字段与否)
    小程序模糊搜索(词汇联想)
    小程序自定义组件的两种方式
    js对数据的一些处理方法(待完善)
    小程序关于登录授权回跳页面的两个问题记录
    小程序登录的一些简单步骤
    关于js的方括号[]属性赋值的一些记录
    js状态转化的简单写法
    微信企业号开发node版
  • 原文地址:https://www.cnblogs.com/z2529827226/p/11621860.html
Copyright © 2020-2023  润新知