C++的堆heap操作RandomIt

2023-12-24 22:39:15

方法

将区间内的元素转化为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 <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
	// 如下两种初始化等价
	// std::vector<int> v = {3, 1, 4, 1, 5, 9};
    std::vector<int> 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<>()
在这里插入图片描述

push_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 << "插入前" << endl;
	display(q);
	// 先 q.push_back 再 push_heap,不要弄反
	q.push_back(12);
	push_heap(q.begin(), q.end(), less<int>());
	cout << "插入后" << endl;
	display(q);
	return 0;
}

pop_heap

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

#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>());
	while(!q.empty()) {
		pop_heap(q.begin(), q.end(), less<int>());
		// 将最后一个位置的堆顶元素及时取走
		q.pop_back();
		display(q);
	}

	return 0;
}

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; // 9 8 6 7 4 5 2 0 3 1 
	display(q);
	sort_heap(q.begin(), q.end(), less<int>());
	cout << "sort后" << endl; // 0 1 2 3 4 5 6 7 8 9 
	display(q);
	return 0;
}

总计

#include <algorithm>
#include <functional>
#include <iostream>
#include <string_view>
#include <vector>
 
void print(std::string text, std::vector<int> const& v = {})
{
    std::cout << text << ": ";
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
int main()
{
    print("Max heap");
 
    std::vector<int> 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<int> 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);
}

文章来源:https://blog.csdn.net/wangkai6666/article/details/135186727
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。