STL之priority queue為復合結構排序

2021-09-22 22:00:16 字數 1958 閱讀 5836

priority_queue為復合結構排序: 

1 #include 2 #include 3

4using

namespace

std;

5struct

node

10};

11bool

operator

<( node a, node b )

16//

自定義過載小於操作符

17int

main()

34while( !q2.empty() )

38while( !q3.empty() )

42//

注意上面不能這樣來定義:priority_queue, greater>;

43//

這樣定義是錯誤的!!!!

44//

原因是:greater沒有定義

4546

//必須定義如下函式物件,才能這樣定義:

47//

priority_queue, cmp >;

48/*

49struct cmp

54};

55*/

56return0;

57}5859 root@u18:~/cp/test# g++ priority.cpp -g -wall

60 root@u18:~/cp/test# valgrind --tool=memcheck --leak-check=yes ./a.out

61 ==24385==memcheck, a memory error detector

d, by julian seward et al.

63 ==24385== using valgrind-3.7.0 and libvex; rerun with -h for

64 ==24385== command: ./a.out

65 ==24385==

66自定義:

6710

1268

91212698

121212707

12121212716

1212121212725

121212121212734

12121212121212743

1212121212121212752

121212121212121212761

12121212121212121212

77 q3 output: 10

78 q3 output: 9

79 q3 output: 8

80 q3 output: 7

81 q3 output: 6

82 q3 output: 5

83 q3 output: 4

84 q3 output: 3

85 q3 output: 2

86 q3 output: 1

87 ==24385==

88 ==24385==heap summary:

89 ==24385== in use at exit: 0 bytes in

0blocks

90 ==24385== total heap usage: 20 allocs, 20 frees, 1,012

bytes allocated

91 ==24385==

92 ==24385== all heap blocks were freed --no leaks are possible

93 ==24385==

94 ==24385== for counts of detected and suppressed errors, rerun with: -v

95 ==24385== error summary: 0 errors from

0 contexts (suppressed: 2

from

2)

stl原始碼分析之priority queue

前面兩篇介紹了gcc4.8的vector和list的原始碼實現,這是stl最常用了兩種序列式容器。除了容器之外,stl還提供了一種借助容器實現特殊操作的元件,謂之介面卡,比如stack,queue,priority queue等,本文就介紹gcc4.8的priority queue的原始碼實現。顧名...

STL容器 優先佇列priority queue

priority queue顧名思義,是乙個具有權值概念的queue,它和queue一樣允許加入新元素 移除舊元素等功能。由於這是乙個queue,所以只允許在底部加入元素,從頂部取出元素。但優先佇列帶有權值概念,其內的元素自動按照元素的權值排序。權值最高者排在最前面。stl的priority que...

STL初步 優先佇列Priority queue

這個優先到底是如何優先?和普通佇列區別在哪?priority queue type,container,functional priority queue,less q priority queue,less a q priority queue,less b 優先佇列中沒有迭代器 也沒有clear...