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();
}

猜你喜欢

转载自www.cnblogs.com/z2529827226/p/11621860.html