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

Posted z2529827226

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

 

以上是关于c++的优先队列的比较函数与Java的比较函数的主要内容,如果未能解决你的问题,请参考以下文章

优先队列比较

优先队列比较

Java Collection - PriorityQueue 优先队列

STL 优先队列的自定义比较函数与 sort() 等泛型算法的自定义比较函数的区别

Java集合与数据结构——优先级队列的使用及练习

与java中的优先级队列混淆