当前位置:网站首页>priority_queue删除
priority_queue删除
2022-07-21 22:20:00 【菜鸟的人工智能之路】
在STL中,priority_queue的底层实现是利用堆。
priority_queue没办法实现O(1)复杂度进行任意元素的删除操作,可用利用两个priority_queue来实现。具体代码如下所示。
push() 给队列中插入元素。
pop() 弹出队首元素。
top() 显示队首元素。
empty() 判断队列是否为空。
erase() 删除队列中任意元素,仅仅是任意元素(即参数为队列中元素)。
#include <queue>
typedef struct PRIORITY_QUEUE {
struct DTYPE {
int x;
int y;
int z;
bool operator == (const DTYPE& temp) const {
return this->x == temp.x && this->y == temp.y && this->z == temp.z;
}
};
struct COMP {
bool operator ()(const DTYPE& a_ele, const DTYPE& b_ele) {
if (a_ele.x == b_ele.x) {
return a_ele.y > b_ele.y;
}
return a_ele.x < b_ele.x;
}
};
priority_queue<DTYPE, vector<DTYPE>, COMP> save_queue, del_queue;
void push(const DTYPE& elememt) {
save_queue.emplace(elememt);
}
bool empty() {
return save_queue.empty();
}
DTYPE top() {
while (!del_queue.empty() && del_queue.top() == save_queue.top()) {
del_queue.pop();
save_queue.pop();
}
DTYPE zero = { NULL, NULL, NULL };
return save_queue.empty() ? zero : save_queue.top();
}
void pop() {
while (!del_queue.empty() && save_queue.top() == del_queue.top()) {
save_queue.pop();
del_queue.pop();
}
if (!save_queue.empty()) {
save_queue.pop();
}
}
void erase(const DTYPE& element) {
del_queue.push(element);
}
void show(const DTYPE& temp) const {
cout << temp.x << " " << temp.y << " " << temp.z << " " << endl;
}
}priqueue;
int main() {
priqueue p_queue;;
p_queue.push({2,1,3});
p_queue.push({ 255,43,3 });
p_queue.push({ 23,1,56 });
p_queue.push({ 212,5,3 });
p_queue.push({ 12,234,234 });
p_queue.push({ 27644,34556,34 });
p_queue.erase({ 212,5,3 });
p_queue.show(p_queue.top());
p_queue.pop();
p_queue.show(p_queue.top());
p_queue.pop();
p_queue.show(p_queue.top());
p_queue.pop();
p_queue.show(p_queue.top());
p_queue.pop();
p_queue.show(p_queue.top());
p_queue.pop();
return 0;
}
边栏推荐
- 2022 音视频技术风向标
- 贵金属入门,有以下四个方法
- Force buckle to distribute biscuits
- [高数] 用定义证明 a 的 1/n 次幂的极限为 1
- 多功能嵌入式解码软件(2)
- 员工激励的几种方式
- [FPGA tutorial case 34] communication case 4 - QPSK modulation signal generation based on FPGA, and its constellation is tested by MATLAB
- JS Console Alert REPL 报错 变量 变量声明提升 数据类型 typeof
- OAuth2.0
- Serial port based SD_ Card system
猜你喜欢
Qgraphicsview graphic view framework uses (VI) primitive animation
多功能嵌入式解码软件(2)
Redis core principles
Chrome插件开发教程
[golang | grpc] use grpc to realize the watch function
LEGO loam run through and source code learning
Exploration of new mode of code free production
C # read and write data to PLC in conjunction with kepserver
A-loam source code reading
[PHP environment setup /wamp/ interpreter / download]
随机推荐
Merge two linked lists
BGP routing principles
Network security risk assessment - best case of landing practice in the telecommunications industry
Working principle of Brushless DC motor
Analysis on DNS domain name hijacking of mobile terminal in IM instant messaging development
华为设备WLAN安全配置命令
等额本金和等额本息还款方式的差异分析
Huawei machine test: folding books
How did the red team carry out the test
Examples of practical application of data security risk assessment in the financial industry
C # split string by string
互联网企业怎样保证自研系统的安全
基于条形码的WMS仓储管理系统有什么优势
Microsoft SDL 2022 latest learning notes
Gb/t 30283-2022 information security technology information security service classification and code learning record
小程序毕设作品之微信酒店预订小程序毕业设计(4)开题报告
Multifunctional embedded decoding software (1)
Detailed analysis process of lapsus stealing Microsoft Bing source code
Exploration of new mode of code free production
121. The best time to buy and sell stocks