杂七杂八的知识

Hutaooo / 2023-08-11 / 原文

1.重载运算符

理解并不难,主要是格式值得注意。
另外要注意在队列中要注意排序的顺序,q.top()/q.front() 访问的是队首元素,如果queue是升序,q.front()就是最大的;

#include <iostream>
#include <queue>
using namespace std;

struct student {
  string name;
  int score;
};

struct cmp {
  bool operator()(const student& a, const student& b) const {
    return a.score < b.score || (a.score == b.score && a.name > b.name);
  }
};

priority_queue<student, vector<student>, cmp> pq;
int main()  {
  return 0;
}

2.sort

当用sort排序vector时,这样写:

sort(vector.begin() , vector.end() , cmp);