STL算法(Algorithms):堆(heap)

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值