1,Make_heap: Turn sequence into heap
Prototype:
Template <class randomaccessiterator>
Void make_heap (randomaccessiterator first, randomaccessiterator last );
Template <class randomaccessiterator, class compare>
Void make_heap (randomaccessiterator first, randomaccessiterator last,
Compare comp );
Example:
1 // range heap example
2 # include <iostream>
3 # include <algorithm>
4 # include <vector>
5 using namespace STD;
6
7int main (){
8 int myints [] = {10, 20, 30, 5, 15 };
9 vector <int> V (myints, myints + 5 );
10
11 make_heap (V. Begin (), V. End ());
12 cout <"Initial Max heap:" <v. Front () <Endl;
13
14 pop_heap (V. Begin (), V. End (); V. pop_back ();
15 cout <"Max heap after pop:" <v. Front () <Endl;
16
17 v. push_back (99); push_heap (V. Begin (), V. End ());
18 cout <"Max heap after push:" <v. Front () <Endl;
19
20 sort_heap (V. Begin (), V. End ());
21
22 cout <"final sorted range :";
23 For (unsigned I = 0; I <v. Size (); I ++) cout <"<V [I];
24
25 cout <Endl;
26
27 return 0;
28}
29
2,Push_heap: Pressure stack (inbound stack)
Prototype:
Template <class randomaccessiterator>
Void push_heap (randomaccessiterator first, randomaccessiterator last );
Template <class randomaccessiterator, class compare>
Void push_heap (randomaccessiterator first, randomaccessiterator last,
Compare comp );
3,Pop_heap: Pop-up stack (Out stack)
Prototype:
Template <class randomaccessiterator>
Void pop_heap (randomaccessiterator first, randomaccessiterator last );
Template <class randomaccessiterator, class compare>
Void pop_heap (randomaccessiterator first, randomaccessiterator last,
Compare comp );
Example:
1 // range heap example
2 # include <iostream>
3 # include <algorithm>
4 # include <vector>
5 using namespace STD;
6
7int main (){
8 int myints [] = {10, 20, 30, 5, 15 };
9 vector <int> V (myints, myints + 5 );
10 vector <int>: iterator it;
11
12 make_heap (V. Begin (), V. End ());
13 cout <"Initial Max heap:" <v. Front () <Endl;
14
15 pop_heap (V. Begin (), V. End (); V. pop_back ();
16 cout <"Max heap after pop:" <v. Front () <Endl;
17
18 V. push_back (99); push_heap (V. Begin (), V. End ());
19 cout <"Max heap after push:" <v. Front () <Endl;
20
21 sort_heap (V. Begin (), V. End ());
22
23 cout <"final sorted range :";
24 For (unsigned I = 0; I <v. Size (); I ++) cout <"<V [I];
25
26 cout <Endl;
27
28 return 0;
29}
30
4,Sort_heap: Sort the heap
Prototype: Template <class randomaccessiterator>
Void sort_heap (randomaccessiterator first, randomaccessiterator last );
Template <class randomaccessiterator, class compare>
Void sort_heap (randomaccessiterator first, randomaccessiterator last,
Compare comp );
Example:
1 // range heap example
2 # include <iostream>
3 # include <algorithm>
4 # include <vector>
5 using namespace STD;
6
7int main (){
8 int myints [] = {10, 20, 30, 5, 15 };
9 vector <int> V (myints, myints + 5 );
10 vector <int>: iterator it;
11
12 make_heap (V. Begin (), V. End ());
13 cout <"Initial Max heap:" <v. Front () <Endl;
14
15 pop_heap (V. Begin (), V. End (); V. pop_back ();
16 cout <"Max heap after pop:" <v. Front () <Endl;
17
18 V. push_back (99); push_heap (V. Begin (), V. End ());
19 cout <"Max heap after push:" <v. Front () <Endl;
20
21 sort_heap (V. Begin (), V. End ());
22
23 cout <"final sorted range :";
24 For (unsigned I = 0; I <v. Size (); I ++) cout <"<V [I];
25
26 cout <Endl;
27
28 return 0;
29}
30