1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 using namespace std; 5 int main() { 6 struct ListNode { 7 int val; 8 ListNode* next; 9 ListNode() : val(0), next(nullptr) {} 10 ListNode(int x) : val(x), next(nullptr) {} 11 ListNode(int x, ListNode* next) : val(x), next(next) {} 12 }; 13 14 struct { 15 bool operator()(ListNode* a, ListNode* b) 16 { 17 return a->val > b->val; 18 } 19 } cmp1; 20 21 auto cmp2 = [](ListNode* a, ListNode* b) 22 { 23 return a->val > b->val; 24 }; 25 26 priority_queue< ListNode*, vector< ListNode*>, decltype(cmp1)> pqClass; 27 priority_queue< ListNode*, vector< ListNode*>, decltype(cmp2)> pqLamda{ cmp2 }; 28 std::vector<int> numbers = { 9, 5, 7, 1, 3 }; 29 for (int num : numbers) { 30 std::cout << num << " "; 31 pqClass.push(new ListNode(num)); 32 } 33 std::cout << std::endl; 34 while (!pqClass.empty()) { 35 std::cout << pqClass.top()->val << " "; 36 pqClass.pop(); 37 } 38 std::cout << std::endl; 39 40 priority_queue<int> pqDefaut; 41 for (int num : numbers) { 42 pqDefaut.push(num); 43 } 44 while (!pqDefaut.empty()) { 45 std::cout << pqDefaut.top()<< " "; 46 pqDefaut.pop(); 47 } 48 std::cout << std::endl; 49 50 priority_queue<int, vector<int>, greater<int>> pqGreater; 51 for (int num : numbers) { 52 pqGreater.push(num); 53 } 54 while (!pqGreater.empty()) { 55 std::cout << pqGreater.top() << " "; 56 pqGreater.pop(); 57 } 58 std::cout << std::endl; 59 60 return 0; 61 }
标签:std,priority,ListNode,自定义,val,int,next,queue,num From: https://www.cnblogs.com/ret0/p/17443405.html