1、
make_heap
:使序列变成堆
原型:
template <class RandomAccessIterator>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
2、 push_heap:压栈(入栈)
原型:
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:弹栈(出栈)
原型:
template <class RandomAccessIterator>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
4、 sort_heap :对堆排序
原型:template <class RandomAccessIterator>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
原型:
template <class RandomAccessIterator>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
1
//
rangeheapexample
2
#include
<
iostream
>
3
#include
<
algorithm
>
4
#include
<
vector
>
5
using
namespace
std;
6
7
int
main()
{
8
intmyints[]={10,20,30,5,15};
9
vector<int>v(myints,myints+5);
10
11
make_heap(v.begin(),v.end());
12
cout<<"initialmaxheap:"<<v.front()<<endl;
13
14
pop_heap(v.begin(),v.end());v.pop_back();
15
cout<<"maxheapafterpop:"<<v.front()<<endl;
16
17
v.push_back(99);push_heap(v.begin(),v.end());
18
cout<<"maxheapafterpush:"<<v.front()<<endl;
19
20
sort_heap(v.begin(),v.end());
21
22
cout<<"finalsortedrange:";
23
for(unsignedi=0;i<v.size();i++)cout<<""<<v[i];
24
25
cout<<endl;
26
27
return0;
28
}
29
//
rangeheapexample
2
#include
<
iostream
>
3
#include
<
algorithm
>
4
#include
<
vector
>
5
using
namespace
std;6

7
int
main()
{8
intmyints[]={10,20,30,5,15};9
vector<int>v(myints,myints+5);10

11
make_heap(v.begin(),v.end());12
cout<<"initialmaxheap:"<<v.front()<<endl;13

14
pop_heap(v.begin(),v.end());v.pop_back();15
cout<<"maxheapafterpop:"<<v.front()<<endl;16

17
v.push_back(99);push_heap(v.begin(),v.end());18
cout<<"maxheapafterpush:"<<v.front()<<endl;19

20
sort_heap(v.begin(),v.end());21

22
cout<<"finalsortedrange:";23
for(unsignedi=0;i<v.size();i++)cout<<""<<v[i];24

25
cout<<endl;26

27
return0;28
}
29
2、 push_heap:压栈(入栈)
原型:
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:弹栈(出栈)
原型:
template <class RandomAccessIterator>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void pop_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
1
//
rangeheapexample
2
#include
<
iostream
>
3
#include
<
algorithm
>
4
#include
<
vector
>
5
using
namespace
std;
6
7
int
main()
{
8
intmyints[]={10,20,30,5,15};
9
vector<int>v(myints,myints+5);
10
vector<int>::iteratorit;
11
12
make_heap(v.begin(),v.end());
13
cout<<"initialmaxheap:"<<v.front()<<endl;
14
15
pop_heap(v.begin(),v.end());v.pop_back();
16
cout<<"maxheapafterpop:"<<v.front()<<endl;
17
18
v.push_back(99);push_heap(v.begin(),v.end());
19
cout<<"maxheapafterpush:"<<v.front()<<endl;
20
21
sort_heap(v.begin(),v.end());
22
23
cout<<"finalsortedrange:";
24
for(unsignedi=0;i<v.size();i++)cout<<""<<v[i];
25
26
cout<<endl;
27
28
return0;
29
}
30
//
rangeheapexample
2
#include
<
iostream
>
3
#include
<
algorithm
>
4
#include
<
vector
>
5
using
namespace
std;6

7
int
main()
{8
intmyints[]={10,20,30,5,15};9
vector<int>v(myints,myints+5);10
vector<int>::iteratorit;11

12
make_heap(v.begin(),v.end());13
cout<<"initialmaxheap:"<<v.front()<<endl;14

15
pop_heap(v.begin(),v.end());v.pop_back();16
cout<<"maxheapafterpop:"<<v.front()<<endl;17

18
v.push_back(99);push_heap(v.begin(),v.end());19
cout<<"maxheapafterpush:"<<v.front()<<endl;20

21
sort_heap(v.begin(),v.end());22

23
cout<<"finalsortedrange:";24
for(unsignedi=0;i<v.size();i++)cout<<""<<v[i];25

26
cout<<endl;27

28
return0;29
}
30
4、 sort_heap :对堆排序
原型:template <class RandomAccessIterator>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
范例:
1
//
rangeheapexample
2
#include
<
iostream
>
3
#include
<
algorithm
>
4
#include
<
vector
>
5
using
namespace
std;
6
7
int
main()
{
8
intmyints[]={10,20,30,5,15};
9
vector<int>v(myints,myints+5);
10
vector<int>::iteratorit;
11
12
make_heap(v.begin(),v.end());
13
cout<<"initialmaxheap:"<<v.front()<<endl;
14
15
pop_heap(v.begin(),v.end());v.pop_back();
16
cout<<"maxheapafterpop:"<<v.front()<<endl;
17
18
v.push_back(99);push_heap(v.begin(),v.end());
19
cout<<"maxheapafterpush:"<<v.front()<<endl;
20
21
sort_heap(v.begin(),v.end());
22
23
cout<<"finalsortedrange:";
24
for(unsignedi=0;i<v.size();i++)cout<<""<<v[i];
25
26
cout<<endl;
27
28
return0;
29
}
30
//
rangeheapexample
2
#include
<
iostream
>
3
#include
<
algorithm
>
4
#include
<
vector
>
5
using
namespace
std;6

7
int
main()
{8
intmyints[]={10,20,30,5,15};9
vector<int>v(myints,myints+5);10
vector<int>::iteratorit;11

12
make_heap(v.begin(),v.end());13
cout<<"initialmaxheap:"<<v.front()<<endl;14

15
pop_heap(v.begin(),v.end());v.pop_back();16
cout<<"maxheapafterpop:"<<v.front()<<endl;17

18
v.push_back(99);push_heap(v.begin(),v.end());19
cout<<"maxheapafterpush:"<<v.front()<<endl;20

21
sort_heap(v.begin(),v.end());22

23
cout<<"finalsortedrange:";24
for(unsignedi=0;i<v.size();i++)cout<<""<<v[i];25

26
cout<<endl;27

28
return0;29
}
30
本文详细介绍了C++标准库中关于堆的四个操作函数:make_heap、push_heap、pop_heap及sort_heap。通过具体代码示例展示了如何使用这些函数来创建、维护和排序堆数据结构。
1310

被折叠的 条评论
为什么被折叠?



