当前位置:网站首页>priority_ Introduction and use of queue
priority_ Introduction and use of queue
2022-07-19 04:15:00 【Hero 2021】
List of articles
One 、priority_queue Introduction to
- Priority queue is a kind of Container adapter , According to the strict weak sorting criteria , Its first element is always the largest of the elements it contains .
- This context is similar to heap , You can insert elements into the heap at any time , And only the largest heap element can be retrieved ( The top element in the priority queue ).
- The priority queue is implemented as a container adapter , The container adapter encapsulates a specific container class as its underlying container class ,queue Provide a specific set of member functions to access its elements . Element from a specific container “ The tail ” eject , It is called the top of the priority queue .
- The underlying container can be any standard container class template , It can also be other specially designed container classes . Containers should be accessible through random access iterators , And supports the following operations :
- empty(): Check whether the container is empty
- size(): Returns the number of valid elements in the container
- front(): Returns a reference to the first element in the container
- push_back(): Insert the element at the end of the container
- pop_back(): Delete the container tail element
- Standard container class vector and deque Meet these needs . By default , If not for a specific priority_queue Class instantiation specifies the container class , Then use vector.
- Need to support random access iterators , So that the heap structure is always maintained internally . The container adapter automatically calls algorithm functions when needed make_heap、push_heap and pop_heap To automatically complete this operation .
- functor / Function object

Two 、priority_queue Use
Priority queues are used by default vector As its underlying container for storing data , stay vector The heap algorithm is used to vector A structure in which elements are constructed in piles , therefore priority_queue Is a pile of , All locations that need to use the heap , You can consider using priority_queue.
Be careful : By default priority_queue It's a lot .
#include<iostream>
#include<queue>
#include<functional>//greater Algorithm header file
using namespace std;
void test_priority_queue()
{
// The default is a lot of —— The default functor is less
//priority_queue<int> pq;
// The priority of small control is high —— Give affine function greater
// Pass in the third template parameter , You must explicitly pass in the second
priority_queue<int,vector<int>,greater<int>> pq;
pq.push(1);
pq.push(2);
pq.push(3);
pq.push(4);
pq.push(5);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
}
int main()
{
test_priority_queue();
return 0;
}
3、 ... and 、priority_quque Of OJ topic

The time complexity of the first two methods is :O(nlogn)
After optimization :O(n+klogn)
But when n When a large , And n Far greater than k, To build a k A few small piles
The following extreme solution :O(nlogk)
边栏推荐
- Structure gets the address of the main structure (struct) through member variables
- 牛客2021训练联盟热身训练赛Interstellar Love(并查集)
- Intel experts share: how to program efficiently on XPU architecture? Zhiqiang Research Institute
- [database] must know at the end of the term ----- Chapter VII database integrity
- 学术分享 | 基于OpenVINO的多染色病理图像信息评估系统设计与开发
- Wechat Online Education video on Demand Learning of applet Graduation Design (3) Background Function
- How to do clear scanning: try scanning tailor scantailor advanced | including the usage of scantailor
- 51 single chip microcomputer - double byte multiplied by double byte
- 笔记本电脑插入耳机仍然外放(亲测有效)
- To build agile teams, these methods are indispensable
猜你喜欢

小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT

Mathematical modeling learning (67): detailed introduction to xgboost classification model case tutorial

小程序毕设作品之微信电子书阅读小程序毕业设计(3)后台功能

PLC OPC 信息模型(DI,PLCopen NodeSets)简介

学术分享 | 基于OpenVINO的多染色病理图像信息评估系统设计与开发

Wechat e-book reading applet graduation design of applet completion works (1) development outline

Introduction au cadre Maui 05 compréhension du modèle de données mvvm

06 MAUI,WPF使用 MVVM Toolkit 框架 构建 MVVM 程序

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(3)后台功能

Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?
随机推荐
Common functions of JMeter - parametric introduction
[super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
小程序毕设作品之微信电子书阅读小程序毕业设计(7)中期检查报告
Common methods of C string
ASP.NET1==visual studio创建asp.net demo
Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
Matlab drawing activation function sigmoid, relu
Micro, m3o micro service series (III)
donet framework4.X==windows窗体应用新建项目,通过System.Data.SqlClient连接sqlserver进行查询
priority_queue的介绍及其使用
Graphic verification code verification
How to use mitmproxy to get data return in automated testing?
minimum spanning tree
C# 构造函数(Constructors)简单讲解
WPF cannot find resource file problem
Heartless sword Chinese English bilingual poem 005 Lyric
VS Code 常用快捷键
In the era of super video, what is the solution to the data flood?
Typescript数组/对象/字符串/函数参数的解构使用
C# 详解out输出参数
