仔细的弄懂了一下,优先队列的情况

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 
 5 struct Node
 6 {
 7     int x;
 8     bool operator <(const Node& b)const{
 9         return x<b.x;
10     }
11     bool operator >(const Node& b)const{
12         return x>b.x;
13     }
14 };
15 
16 int main()
17 {
18     // 其余的也就这么改就行了
19     priority_queue<Node> Q1; // 结构体默认排序,权大先的出队
20     priority_queue<Node ,vector<Node> ,greater<Node> > Q2; // 再定义出队顺序,权小的先出队
21 
22     for (int i=1;i<=10;i++)
23     Q2.push((Node){i});
24     while (!Q2.empty())
25     {
26         int x = Q2.top().x; Q2.pop(); // Q.size() 队列长度
27         cout <<x<<' ';
28     }
29     return 0;
30 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-01-21
  • 2021-12-10
  • 2021-08-09
  • 2021-08-19
  • 2022-01-07
  • 2021-11-20
猜你喜欢
  • 2021-09-27
  • 2021-12-13
  • 2021-11-07
  • 2021-09-27
  • 2021-09-27
  • 2021-11-22
  • 2021-07-18
相关资源
相似解决方案