用set实现大顶堆
来源:互联网 发布:图片上传淘宝变模糊 编辑:程序博客网 时间:2024/06/06 02:29
红黑树通过把节点分为红黑两个颜色并根据一些规则确保树在一定程度上是平衡的,从而保证红黑树中查找、删除和插入操作都只需要O(logk
)时间。而在set和multiset都是基于红黑树实现的。
<span style="background-color: rgb(255, 252, 246);">set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就 像一个集合一样。所有的操作的都是严格在logn时间之内完成,效率非常高。 set和multiset的区别是:</span><span style="color: rgb(255, 0, 0);">set插入的元素不能相同,但是multiset可以相同<span style="background-color: rgb(255, 252, 246);">。</span></span>
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(255, 252, 246);"></span></span><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 25.9999980926514px;">1. <strong><span style="color: rgb(255, 102, 102);">关于运算符重载:</span>h</strong>ttp://blog.csdn.net/candy20094369/article/details/6749171</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 25.9999980926514px;">2. multiset常用操作:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 25.9999980926514px;"><span style="color: rgb(255, 0, 0);">insert() 在集合中插入元素begin() 返回指向第一个元素的迭代器,end() 返回指向最后一个元素的迭代器clear() 清除所有元素</span>count() 返回某个值元素的个数 (如果是set,返回值不是1,就是0)empty() 如果集合为空,返回trueequal_range() 返回集合中与给定值相等的上下限的两个迭代器erase() 删除集合中的元素find() 返回一个指向被查找到元素的迭代器get_allocator() 返回集合的分配器ower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器key_comp() 返回一个用于元素间值比较的函数max_size() 返回集合能容纳的元素的最大限值rbegin() 返回指向集合中最后一个元素的反向迭代器rend() 返回指向集合中第一个元素的反向迭代器size() 集合中元素的数目swap() 交换两个集合变量upper_bound() 返回大于某个值元素的迭代器value_comp() 返回一个用于比较元素间的值的函数</p><div></div>typedef struct{<span style="white-space:pre"></span>int index;<span style="white-space:pre"></span>double length;}Info;typedef struct{<span style="white-space:pre"></span>bool operator()(const Info& info1, const Info& info2){<span style="white-space:pre"></span>if ((info1.length - info2.length) > 0)<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>}}Comp;typedef multiset<Info,Comp> intSet;void GetLeastNumbers(const vector<Info>& data,int length, int k, intSet& result){<span style="white-space:pre"></span>result.clear();<span style="white-space:pre"></span>for(int index = 0;index <length ;index++){<span style="white-space:pre"></span>if (result.size()<k)<span style="white-space:pre"></span>result.insert(data[index]);<span style="white-space:pre"></span>else{<span style="white-space:pre"></span>if (data[index].length < result.begin()->length){<span style="white-space:pre"></span>result.erase(result.begin());<span style="white-space:pre"></span>result.insert(data[index]);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(255, 252, 246);">multiset<int, greater<int>> intset;</span></span>
<span style="color:#ff0000;"><span style="background-color: rgb(255, 252, 246);">可以简单实现大顶堆,如果采用less<int>可以实现小顶堆。</span></span>
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(255, 252, 246);"></span></span><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 25.9999980926514px;"><span style="font-family: simsun; line-height: 20.6666660308838px;">priority_q</span><span style="font-family: simsun; line-height: 20.6666660308838px;">ueue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法</span><span style="font-family: simsun; line-height: 20.6666660308838px;">实现,也算是堆的另外一种形式。与heap一样,都调用</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 25.9999980926514px;"><span style="font-family: simsun;"><span style="line-height: 20.6333332061768px;">#include<algorithm></span></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 25.9999980926514px;"><span style="font-family: simsun; line-height: 20.6666660308838px;"><span style="line-height: 20.6666660308838px;">priority_queue 对于基本类型的使用方法相对简单。</span><br style="line-height: 20.6666660308838px;" /><span style="line-height: 20.6666660308838px;">他的模板声明带有三个参数,priority_queue<Type, Container, Functional></span><br style="line-height: 20.6666660308838px;" /><span style="line-height: 20.6666660308838px;">Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。</span><br style="line-height: 20.6666660308838px;" /><span style="line-height: 20.6666660308838px;">Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list. </span><span style="line-height: 20.6666660308838px;">STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩</span><span style="line-height: 20.6666660308838px;">参数缺省的话,优先队列就是大顶堆,队头元素最大。</span></span></p>typedef priority_queue<Info, vector<Info>,Comp> intPriority;
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(255, 252, 246);">typedef struct{ bool operator()(const Info& info1, const Info& info2){ if ((info1.length - info2.length)< 0) return true; return false; } }Comp;void GetLeastNumbers(const vector<Info>& data,int length, int k, intPriority& result){ for(int index = 0;index <length ;index++){ <span style="white-space:pre"></span>if (result.size()>0)<span style="white-space:pre"></span>Info temp = result.top(); if (result.size()<k) result.push(data[index]); else{ <span style="white-space:pre"></span> if (data[index].length < result.top().length){ result.pop(); result.push(data[index]); } } } } </span></span>
0 0
- 用set实现大顶堆
- 用set实现的极大团
- UVA 10391Compound Words 用set实现
- 用Redis的set指令实现锁
- JavaScript Set简单实现
- set集合实现排序
- set容器的实现
- Set接口实现类
- Set--TreeSet实现分析
- STL之set实现
- c实现set集合
- C++ 实现 get set
- Set集合的实现
- 【Java】 Set实现类
- set的底层实现
- set底层实现方式?
- golang实现set数据结构
- 3729:用set实现字符串的排序和查找
- EGORefreshTableHeaderView使用方法(二)
- Android环境搭建Appium测试框架 Python
- 分布式系统一致性的发展历史
- iOS中关于属性@property和@synthesize的问题:
- Git使用教程1:下载及安装
- 用set实现大顶堆
- js获得表格多行数据(多选框)
- qt model 与 view 的数据显示
- Linux date命令的用法
- 架构设计:系统间通信(6)——IO通信模型和Netty 上篇
- linux c获取股票脚本
- [iOS学习笔记]自学过程中积累的知识点(五)
- Mac下的抓包利器--Charles的安装与使用
- 如何判断ios设备类型