STL的priorit_queue类

本文介绍了STL中的priority_queue容器及其使用方法,并展示了如何利用STL中的堆算法进行高效的数据处理。通过实例代码,读者可以了解到如何创建优先队列并使用堆算法对容器进行操作。

STL提供了priority_queue类,它是一个适配器容器。默认的基本容器类型是vector。priority_queue可以插入元素,但不能遍历元素。只能访问最上面的元素。元素在队列中出入时。会把优先级最高的元素放在队首,优先级最高的元素通过默认运算符<或比较函数对象来确定。priority_queue容器使用随机访问迭代器,采用支持front、push_back和pop_back 的容器,例如deque和vector。priority_queue最好实现为堆。下面是STL中priority_queue容器的一个简化规范:

template<class T ,class Container=vector<T>, class Compare=less<typename Container::value_type>> class priority_queue { public: explicit priority_queue(const Compare &=Compare(), const Container &=Container); //default constructor ;initializes an empty priority queue //A comparison function object may be supplied //precondition :none //postcondition :returns true if the priority queue is empty //otherwise returns false size_type size()const; //determines the size of the priority queue //the return type size_type is an integal type //precondition :none //postcondition:returns the nubmer of items that //are currently in the priority queue const value_type &top()const; //returns a reference to the highest priority element //in the priority queue //precondition :none //postcondition :the item remains at the top of the //priority queue void pop(); //remove the highest priority element in the priority queue //precondition :none //postcondition :the priority queue has the highest //priority element at the top void push(const value_type &e); //Adds the item e to the priority queue . //precondition:none //postcondition :the priority queue has the highest //priority element at the top };


STL还在<algorithm>中提供了堆算法。容器可以使用make_heap函数转换为堆,在使用sort_heap转换回容器。push_heap和pop_heap函数可以添加和删除堆的第一个元素,钙元素的优先级最高。堆算法要求使用随机访问迭代器。函数规范如下所示:

void push_heap(RandomIter first,RandomIter last); void push_heap(RandomIter first,RandomIter last,Compare cmp); //push an item onto the heap //The value pushed is *(last-1) //A comparison function object may be supplied. void pop_heap(RandomIter first,RandomIter last); void pop_heap(RandomITer first,RandomIter last,Compare cmp); //pop an item onto the heap //swaps first element with *(last-1) and makes [first ,last-1] into a heap //A compare function object may be supplied void make_heap(RandomIter first,RandomIter second); void make_heap(RandomIter first,RandomIter second,Compare cmp); //Turns an existing container into a heap //A compare function object may be supplied void sort_heap(RandomIter first,RandomIter second); void sort_heap(RandomIter first ,RandomIter second,Compare cmp); //turns the heap back into original container //A comparison function may be supplied


下面的程序演示了优先队列的用法和一个使用堆的操作的vector:

#include<algorithm> #include<vector> #include<iostream> #include<cstdlib> #include<ctime> #include<queue> using namespace std; int main() { //declare a priority queue priority_queue<int,vector<int> > pq; //declare a vector vector<int> vheap; //declare a vector iterator vector<int>::iterator iter; //seed the random number generator srand(time(0)); //fill the priority queue and vector with random numbers //push each nubmer of vector with random numbers //using the greater predicate; for(int i=0;i<25;i++) { int j=rand()%25; pq.push(j); vheap.push_back(j); push_heap(vheap.begin(),vheap.end(),greater<int>()); } //show the generated numbers in their original order by //iterating through the vector cout<<"original numbers :"<<endl; for(iter=vheap.begin();iter!=vheap.end();iter++) { cout<<*iter<<" "; } //display the priority queue by popping the top off cout<<endl<<" Priority queue :"<<endl; while(!pq.empty()) { cout<<pq.top()<<" "; pq.pop(); } //display the vector as a heap by poping the top off cout<<endl<<" Heap :"<<endl; while(!vheap.empty()) { cout<<vheap[0]<<" "; pop_heap(vheap.begin(),vheap.end(),greater<int>()); vheap.pop_back(); } cout<<endl; return 0; }


内容概要:本文以一款电商Android应用为案例,系统讲解了在Android Studio环境下进行性能优化的全过程。文章首先分析了常见的性能问题,如卡顿、内存泄漏和启动缓慢,并深入探讨其成因;随后介绍了Android Studio提供的三大性能分析工具——CPU Profiler、Memory Profiler和Network Profiler的使用方法;接着通过实际项目,详细展示了从代码、布局、内存到图片四个维度的具体优化措施,包括异步处理网络请求、算法优化、使用ConstraintLayout减少布局层级、修复内存泄漏、图片压缩与缓存等;最后通过启动时间、帧率和内存占用的数据对比,验证了优化效果显著,应用启动时间缩短60%,帧率提升至接近60fps,内存占用明显下降并趋于稳定。; 适合人群:具备一定Android开发经验,熟悉基本组件和Java/Kotlin语言,工作1-3年的移动端研发人员。; 使用场景及目标:①学习如何使用Android Studio内置性能工具定位卡顿、内存泄漏和启动慢等问题;②掌握从代码、布局、内存、图片等方面进行综合性能优化的实战方法;③提升应用用户体验,增强应用稳定性与竞争力。; 阅读建议:此资源以真实项目为背景,强调理论与实践结合,建议读者边阅读边动手复现文中提到的工具使用和优化代码,并结合自身项目进行性能检测与调优,深入理解每项优化背后的原理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值