码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-10-04 13:39:52      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:system   output   input   main   func   oid   solution   compareto   file   

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的比较函数

标签:system   output   input   main   func   oid   solution   compareto   file   

原文地址:https://www.cnblogs.com/z2529827226/p/11621860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!