C++的堆heap操作RandomIt

方法

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

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

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

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

测试范围内的元素是否是一个二叉堆(C++11)
is_heap

C++11新增特性
返回有效二叉堆的最末范围。如果都有效,则返回last.也就是说,返回第一个破坏二叉堆结构元素的迭代器
is_heap_until

is_heap_until

参数是迭代器,返回位置也是迭代器,指向第一个不符合大顶堆的元素

#include 
#include 
#include 
 
int main()
{
	// 如下两种初始化等价
	// std::vector v = {3, 1, 4, 1, 5, 9};
    std::vector v{3, 1, 4, 1, 5, 9};
 
    std::make_heap(v.begin(), v.end());
 
    // probably mess up the heap
	// 如下元素可能会弄乱heap
    v.push_back(2);
    v.push_back(6);
 
    auto heap_end = std::is_heap_until(v.begin(), v.end());
 
 	// 9 5 4 1 1 3 2 6
    std::cout << "all of v:  ";
    for (const auto& i : v)
        std::cout << i << ' ';
    std::cout << '\n';
 
 	// 9 5 4 1 1 3 2
    std::cout << "only heap: ";
    for (auto i = v.begin(); i != heap_end; ++i)
        std::cout << *i << ' ';
    std::cout << '\n';
}

make_heap

将RandomIt的[first, last)之间的元素进行大顶堆(默认)构建
还可以手动设置小顶堆

第三个参数是less<>()或是greater<>(),前者用于生成大顶堆,后者用于生成小顶堆
第三个参数默认情况下为less<>(),less()用于生成大顶堆
对应头文件#include ,且一定要加括号less<>()
C++的堆heap操作RandomIt_第1张图片

push_heap

#include
#include
#include
#include 
#include 
using namespace std;

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

int main() {
	vector q;
	for (int i = 0; i < 10; i++) {
		q.push_back(i);
	}
	make_heap(q.begin(), q.end(), less());
	cout << "插入前" << endl;
	display(q);
	// 先 q.push_back 再 push_heap,不要弄反
	q.push_back(12);
	push_heap(q.begin(), q.end(), less());
	cout << "插入后" << endl;
	display(q);
	return 0;
}

pop_heap

将堆顶部元素和最后一个位置的元素进行交换

#include 

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

int main()
{
	vector q;
	for (int i = 0; i < 10; i++) {
		q.push_back(i);
	}
	
	make_heap(q.begin(), q.end(), less());
	while(!q.empty()) {
		pop_heap(q.begin(), q.end(), less());
		// 将最后一个位置的堆顶元素及时取走
		q.pop_back();
		display(q);
	}

	return 0;
}

sort_heap

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

#include
#include
#include
#include 
#include 
using namespace std;
void display(vectorq)
{
	for (int i = 0; i < q.size(); i++) {
		cout << q[i] << " ";
	}
	cout << endl;
}
int main()
{
	vector q;
	for (int i = 0; i < 10; i++) {
		q.push_back(i);
	}
	make_heap(q.begin(), q.end(), less());
	cout << "sort前" << endl; // 9 8 6 7 4 5 2 0 3 1 
	display(q);
	sort_heap(q.begin(), q.end(), less());
	cout << "sort后" << endl; // 0 1 2 3 4 5 6 7 8 9 
	display(q);
	return 0;
}

总计

#include 
#include 
#include 
#include 
#include 
 
void print(std::string text, std::vector const& v = {})
{
    std::cout << text << ": ";
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
int main()
{
    print("Max heap");
 
    std::vector v{3, 2, 4, 1, 5, 9};
    print("initially, v", v);
 
    std::make_heap(v.begin(), v.end());
    print("after make_heap, v", v);
 
    std::pop_heap(v.begin(), v.end());
    print("after pop_heap, v", v);
 
    auto top = v.back();
    v.pop_back();
    print("former top element", {top});
    print("after removing the former top element, v", v);
 
    print("\nMin heap");
 
    std::vector v1{3, 2, 4, 1, 5, 9};
    print("initially, v1", v1);
 
    std::make_heap(v1.begin(), v1.end(), std::greater<>{});
    print("after make_heap, v1", v1);
 
    std::pop_heap(v1.begin(), v1.end(), std::greater<>{});
    print("after pop_heap, v1", v1);
 
    auto top1 = v1.back();
    v1.pop_back();
    print("former top element", {top1});
    print("after removing the former top element, v1", v1);
}

你可能感兴趣的:(C++,c++)