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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql opkg源_如何修改opkg源

    mysql opkg源_如何修改opkg源修改opkg源vimopkg.conf##对应路径替换为如下:src/gzopenwrt_corehttp://mirrors.ustc.edu.cn/lede/releases/18.06.4/targets/x86/64/packagessrc/gzopenwrt_basehttp://mirrors.ustc.edu.cn/lede/releases/18.06.4/packag…

    2022年6月9日
    54
  • JSP页面通过URL传递中文参数出现中文乱码问题

    JSP页面通过URL传递中文参数出现中文乱码问题做项目时遇到这个问题问题描述:通过点击这个参数名并进行跳转control层,进行参数接受并处理。${cs.name}里面包含中文&lt;a href="${pageContext.request.contextPath}/recommend_navCategory?name=${cs.name}"&gt;${cs.name}&lt;/a&gt;服务器端:ISO-8859-1是tomca…

    2022年6月13日
    30
  • DB2 数据库分区表语法[通俗易懂]

    DB2 数据库分区表语法[通俗易懂]CREATETABLE语句的PARTITIONBY子句指定了表数据的分区。该定义中使用的列被称为表分区键列。

    2022年5月3日
    90
  • 科学计算编程语言_中国发明了什么编程语言

    科学计算编程语言_中国发明了什么编程语言本文是《打破国外垄断,开发中国人自己的编程语言》系列文章的第1篇。本系列文章的主要目的是教大家学会如何从零开始设计一种编程语言(marvel语言),并使用marvel语言开发一些真实的项目,如移动App、Web应用等。

    2022年10月5日
    2
  • 10种流行的Java框架[通俗易懂]

    10种流行的Java框架[通俗易懂]任何框架都是有助于更快更好地开发软件解决方案的工具之一。框架的基本原理不必重新发明轮子。框架使开发人员的工作变得更轻松,并帮助他们专注于业务逻辑,而不必担心通用的代码段。而且由于Java并不是最简单的编程语言之一,因此框架在这里绝对是有用的工具。在本文中,我收集了一些最流行,最有价值的框架,这些框架可以帮助您进行Java应用程序开发。1.Spring这是其他Java框架中的绝对领导者。掌握Spring是Java开发人员职位最普遍的要求之一。造成这种情况的原因很多,但主要的.

    2022年7月7日
    30
  • “双击Pycharm无响应”解决方案「建议收藏」

    “双击Pycharm无响应”解决方案「建议收藏」问题描述昨晚直接关机,导致pycharm强制关闭,今早打开时双击图标无响应解决方法第一步:找到该路径下的cmd.exe,右键管理员身份打开;第二步:在cmd窗口中,输入netshwinsockreset,回车;第三步:重启电脑,尝试重新打开pycharm关于指令winsock是Windows网络编程接口netsh是一个能够通过命令行操作几乎所有网络相关设置的接口netshwinsockreset命令,作用是重置Winsock目录这个命令可以重新初始化网

    2022年8月28日
    7

发表回复

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

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