【C++】STL中heap函数的用法(make_heap,push_heap,pop_heap,sort_heap,is_heap,is_heap_until)...

简介

C++

make_heap()将区间内的元素转化为heap.

push_heap()对heap增加一个元素.

pop_heap()对heap取出下一个元素.

sort_heap()对heap转化为一个已排序群集.

C++11新增特性

is_heap            测试范围内的元素是否是一个二叉堆

is_heap_until   该函数返回有效二叉堆的最末范围。如果都有效,则返回last.也就是说,返回第一个破坏二叉堆结构元素的迭代器。

例如一个序列:9 8 7 6  10 5 9

画成堆的结构如下:

bubuko.com,布布扣

可以看到,10是第一个破坏堆结构的元素,9也是,运行程序:

 

函数

1.make_heap()

make_heap()用于把一个可迭代容器变成一个堆,默认是大顶堆。

它有三个参数。第一个参数是指向开始元素的迭代器,第二个参数是指向最末尾元素的迭代器,第三个参数是less<>()或是greater<>(),前者用于生成大顶堆,后者用于生成小顶堆,第三个参数默认情况下为less<>(),less<int>()用于生成大顶堆。

要使用less<int>(),以及greater<int>(),请添加头文件#include <functional>,且一定要加括号less<>()

#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>
using namespace std;

int main()
{
    vector<int> q;

    for (int i = 0; i < 10; i++)
    {
        q.push_back(i);
    }

    make_heap(q.begin(), q.end(), less<int>());

    for (int i = 0; i < q.size(); i++)
    {
        cout << q[i] << " ";
    }

    return 0;
}

2.pop_heap()

pop_heap()用于将堆的第零个元素与最后一个元素交换位置,然后针对前n - 1个元素调用make_heap()函数,它也有三个参数,参数意义与make_heap()相同,第三个参数应与make_heap时的第三个参数保持一致。

注意:pop_heap()函数,只是交换了两个数据的位置,如果需要弹出这个数据,请记得在pop_heap()后加上

q.pop_back();

#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>

using namespace std;
void display(vector<int>q)
{
    for (int i = 0; i < q.size(); i++)
    {
        cout << q[i] << " ";
    }
    cout << endl;
}

int main()
{
    vector<int> q;
    for (int i = 0; i < 10; i++)
    {
        q.push_back(i);
    }
    make_heap(q.begin(), q.end(), less<int>());
    display(q);
    pop_heap(q.begin(), q.end(), less<int>());
    display(q);

    return 0;
}

3.push_heap()

push_heap()用于把数据插入到堆中,它也有三个参数,其意义与make_heap()的相同,第三个参数应与make_heap时的第三个参数保持一致。

在使用push_heap()前,请确保已经把数据通过q.push_back()传入q中,而不是在push_heap()后再使用q.push_back(t)!!

#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>
using namespace std;
void display(vector<int>q)
{
    for (int i = 0; i < q.size(); i++)
    {
        cout << q[i] << " ";
    }
    cout << endl;
}
int main()
{
    vector<int> q;
    for (int i = 0; i < 10; i++)
    {
        q.push_back(i);
    }
    make_heap(q.begin(), q.end(), less<int>());
    cout << "插入前" << endl;
    display(q);
    q.push_back(12);
    push_heap(q.begin(), q.end(), less<int>());
    cout << "插入后" << endl;
    display(q);
    return 0;
}

4.sort_heap()

sort_heap()是将堆进行排序,排序后,序列将失去堆的特性(子节点的键值总是小于或大于它的父节点)。它也具有三个参数,参数意义与make_heap()相同,第三个参数应与make_heap时的第三个参数保持一致。大顶堆sort_heap()后是一个递增序列,小顶堆是一个递减序列。

请在使用这个函数前,确定序列符合堆的特性,否则会报错!

#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <functional>
using namespace std;
void display(vector<int>q)
{
    for (int i = 0; i < q.size(); i++)
    {
        cout << q[i] << " ";
    }
    cout << endl;
}
int main()
{
    vector<int> q;
    for (int i = 0; i < 10; i++)
    {
        q.push_back(i);
    }
    make_heap(q.begin(), q.end(), less<int>());
    cout << "sort前" << endl;
    display(q);
    sort_heap(q.begin(), q.end(), less<int>());
    cout << "sort后" << endl;
    display(q);
    return 0;
}

原文:https://www.cnblogs.com/woxiaosade/p/10628388.html

例子

template<typename T>
inline void INSERT_ELEMENTS(T &coll, int first, int last)
{
    for (int i = first; i <= last; ++i)
    {
        coll.insert(coll.end(), i);
    }
}


template<typename T>
inline void PRINT_ELEMENTS(const T &coll, const string &optcstr = "")
{
    cout << optcstr;
    for (auto elem : coll)
    {
        cout << elem << ' ';
    }
    cout << endl;
}

int main()
{

    vector<int> a;
    INSERT_ELEMENTS(a, 3, 7);
    INSERT_ELEMENTS(a, 5, 9);
    INSERT_ELEMENTS(a, 1, 4);
    PRINT_ELEMENTS(a, "on entry:	");

    make_heap(a.begin(), a.end());
    PRINT_ELEMENTS(a, "after make_heap():	");

    pop_heap(a.begin(), a.end());
    a.pop_back();

    PRINT_ELEMENTS(a, "after pop_heap():	");
    a.push_back(17);
    push_heap(a.begin(), a.end());
    PRINT_ELEMENTS(a, "after push_heap():	");
    sort_heap(a.begin(), a.end());
    PRINT_ELEMENTS(a, "after sort_heap():	");
}
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;

void isheap()
{
    vector<int> vi{1, 2, 3, 4, 5, 6, 7};
    vector<int> v2(vi.rbegin(), vi.rend());
    cout << "vi=";
    for_each(vi.begin(), vi.end(), [](int i)
    {
        cout << i << " ";
    });
    cout << endl;

    if(is_heap(vi.begin(), vi.end()))
        cout << "is_heap(vi.begin(),vi.end()) return true!" << endl;
    else
        cout << "is_heap(vi.begin(),vi.end()) return false!" << endl;

    cout << "v2=";
    for_each(v2.begin(), v2.end(), [](int i)
    {
        cout << i << " ";
    });
    cout << endl;

    if(is_heap(v2.begin(), v2.end()))
        cout << "is_heap(v2.begin(),v2.end()) return true!" << endl;
    else
        cout << "is_heap(v2.begin(),v2.end()) return false!" << endl;

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值