STL algorithm (algorithms): heap)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.