c++ 优先级队列_kafka优先级队列

c++ 优先级队列_kafka优先级队列C++优先级队列解析优先级队列:是零个或多个元素的集合,优先级队列中每一个元素都有一个优先级,元素的先后的出队顺序是由优先级的高低决定的。优先级高的先出队,优先级低的后出队。优先级队列的主要特点:从一个集合中能够快速的查找到和删除最大值和最小值的元素。1.入队解释图:2.出队解释图:3.代码:PriorityQueue.h#pragmaonce#ifndefMYPRIORITYQUEUE_H#defineMYPRIORITYQUEUE_H#include<iostre

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

C++优先级队列解析

优先级队列:是零个或多个元素的集合,优先级队列中每一个元素都有一个优先级,元素的先后的出队顺序是由优先级的高低决定的。优先级高的先出队,优先级低的后出队。

优先级队列的主要特点:从一个集合中能够快速的查找到和删除最大值和最小值的元素。

1.入队解释图:

在这里插入图片描述
在这里插入图片描述

2.出队解释图:

在这里插入图片描述
在这里插入图片描述

3.代码:

PriorityQueue.h

#pragma once
#ifndef MYPRIORITYQUEUE_H
#define MYPRIORITYQUEUE_H
#include<iostream>
using namespace std;
template<class T>
class MyPriorityQueue
{ 
   
public:
	MyPriorityQueue();
	void inQueue(const T & value);
	void outQueue();
	T topQueue();
	~MyPriorityQueue();


	void siftDown(int pos);
	void siftUp(int pos);
	void resize();
	T * data;
	int size;
	int Max_size;
};

#endif // !MYPRIORITYQUEUE_H


PriorityQueue.cpp递减

#include"PriorityQueue.h"
template<class T>
MyPriorityQueue<T>::MyPriorityQueue()
{ 
   
	this->size = 0;
	this->Max_size = 1024;
	data = new T[Max_size];
}
template<class T>
void MyPriorityQueue<T>::inQueue(const T &value)
{ 
   
	if (size==Max_size-1)
	{ 
   
		resize();
	}
	data[++size] = value;
	siftUp(size);
}
template<class T>
void MyPriorityQueue<T>::outQueue()
{ 
   
	if (size==0)
	{ 
   
		return;
	}
	T temp;
	temp = data[1];
	data[1] = data[size--];
	siftDown(1);
}
template<class T>
T MyPriorityQueue<T>::topQueue()
{ 
   
	if (size==0)
	{ 
   
		return NULL;
	}
	return data[1];
}
template<class T>
MyPriorityQueue<T>::~MyPriorityQueue()
{ 
   
	delete data;
}
template<class T>
void MyPriorityQueue<T>::siftDown(int pos)
{ 
   
	T temp = data[pos];
	int child;
	for (;pos*2<=size;pos=child)
	{ 
   
		child = pos * 2;
		if (child !=size&&data[child +1]<data[child])
		{ 
   
			child = child + 1;
		}
		if (temp>data[child])
		{ 
   
			data[pos] = data[child];
		}
		else
		{ 
   
			break;
		}
	}
	data[pos] = temp;
}
template<class T>
void MyPriorityQueue<T>::siftUp(int pos)
{ 
   
	T temp = data[pos];
	for (;pos>1&&temp<data[pos/2];pos/=2)
	{ 
   
		data[pos] = data[pos / 2];
	}
	data[pos] = temp;
}
template<class T>
void MyPriorityQueue<T>::resize()
{ 
   
	Max_size = Max_size * 2;
}

PriorityQueue.cpp递增

#include"PriorityQueue.h"
template<class T>
MyPriorityQueue<T>::MyPriorityQueue()
{ 
   
	this->size = 0;
	this->Max_size = 1024;
	data = new T[Max_size];
}
template<class T>
void MyPriorityQueue<T>::inQueue(const T &value)
{ 
   
	if (size==Max_size-1)
	{ 
   
		resize();
	}
	data[++size] = value;
	siftUp(size);
}
template<class T>
void MyPriorityQueue<T>::outQueue()
{ 
   
	if (size==0)
	{ 
   
		return;
	}
	T temp;
	temp = data[1];
	data[1] = data[size--];
	siftDown(1);
}
template<class T>
T MyPriorityQueue<T>::topQueue()
{ 
   
	if (size==0)
	{ 
   
		return NULL;
	}
	return data[1];
}
template<class T>
MyPriorityQueue<T>::~MyPriorityQueue()
{ 
   
	delete data;
}
template<class T>
void MyPriorityQueue<T>::siftDown(int pos)
{ 
   
	T temp = data[pos];
	int child;
	for (;pos*2<=size;pos=child)
	{ 
   
		child = pos * 2;
		if (child !=size&&data[child +1]>data[child])
		{ 
   
			child = child + 1;
		}
		if (temp<data[child])
		{ 
   
			data[pos] = data[child];
		}
		else
		{ 
   
			break;
		}
	}
	data[pos] = temp;
}
template<class T>
void MyPriorityQueue<T>::siftUp(int pos)
{ 
   
	T temp = data[pos];
	for (;pos>1&&temp>data[pos/2];pos/=2)
	{ 
   
		data[pos] = data[pos / 2];
	}
	data[pos] = temp;
}
template<class T>
void MyPriorityQueue<T>::resize()
{ 
   
	Max_size = Max_size * 2;
}

main.cpp
注意此处使用的是.cpp头文件,不能使用.h头文件了,不要回连接处错误。因为此处用的还是模板类

#include"PriorityQueue.cpp" 
int main()
{ 
   
	MyPriorityQueue<int> pq;
	pq.inQueue(2);
	pq.inQueue(5);
	pq.inQueue(10);
	pq.inQueue(4);
	while (pq.size!=0)
	{ 
   
		std::cout << pq.topQueue() << " ";
		pq.outQueue();
	}
	system("pause");
	return 0;
}

4.结果:

在这里插入图片描述

5.本地优先级队列API

其实在C++的queue库中有优先级队列的接口API

使用时要包含头文件#include <queue>

基本操作:
top 访问队头元素
empty 队列是否为空
size 返回队列内元素个数
push 插入元素到队尾 (并排序)
emplace 原地构造一个元素并插入队列
pop 弹出队头元素
swap 交换内容
//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;

代码:

#include<iostream>
#include<queue>
using namespace std;
int main()
{ 
   
	priority_queue<char,vector<char>,less<char>> a;
	a.push('b');
	a.push('c');
	a.push('a');
	a.push('s');
	while (!a.empty())
	{ 
   
		cout << a.top() << " ";
		a.pop();
	}
	system("pause");
	return 0;
}

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190469.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • poj2488 A Knight’s Journey

    poj2488 A Knight’s Journey

    2021年5月27日
  • 向量三重积的几何意义_向量三重积

    向量三重积的几何意义_向量三重积定义同一个线性空间可定义不同的内积。选择复线性空间上的内积实内积空间性质向量长度向量长度性质Cauchy-Schwartz不等式Cauchy-Schwartz不等式推论度量矩阵只要告诉一组基下任意两个向量的内积,就会形成一个度量矩阵。那么随便拿一个向量,都知道它的坐标,这两个向量的内积就是右边的xTGyx^TGyxTGy。如果GGG为单位矩阵,那么<a,b><a,b><a,b>内积就等于他们坐标的内积。G为单位矩阵

    2022年8月30日
  • Wlan与WiFi[通俗易懂]

    Wlan与WiFi[通俗易懂]首先我们简单介绍下WLAN无线上网,其全称是:Wireless Local Area Networks,中文解释为:无线局域网络,是一种利用射频(Radio Frequency RF)技术进行据传输的系统,该技术的出现绝不是用来取代有线局域网络,而是用来弥补有线局域网络之不足,以达到网络延伸之目的,使得无线局域网络能利用 简单的存取架构让用户透过它,实现无网线、无距离限制的通畅网络。WLAN 使用

    2022年7月11日
  • API接口重复提交

    API接口重复提交

    2021年11月6日
  • 免备案CDN推荐+教程

    免备案CDN推荐+教程文章目录前言一、CDN1.注册2.配置二、域名解析三、Nginx配置修改前言最近在搭建个人博客,到备案的时候发现困难重重(很麻烦),然后就想着用点其他法子。于是乎网上搜到了这个免备案CDN,花了一会部署完成后,感觉还不错就记录下来。附上我的博客链接:chasezc,功能还不是很完善,还在继续补充。一、CDN在网上查到的这个叫Nodecache,主要提供CDN、DNS、SSL证书等业务,亚太节点有我们喜欢的香港线路,速度很不错。1.注册首先我们去官网上注册:https://www.node

    2022年9月11日
  • 编写WordCount程序之一固定格式讲解

    编写WordCount程序之一固定格式讲解

    2022年3月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号