当前位置: 首页 > news >正文

中山古镇做网站农村电商平台

中山古镇做网站,农村电商平台,深圳做网站公司有哪些企业,uc导航介绍完了stack和queue的介绍以及模拟的相关内容后:C初阶:容器适配器介绍、stack和queue常用接口详解及模拟实现 接下来进行priority_queue的介绍以及模拟: 文章目录 1.priority_queue的介绍和使用1.1priority_queue的初步介绍1.2priority_que…

介绍完了stack和queue的介绍以及模拟的相关内容后:C++初阶:容器适配器介绍、stack和queue常用接口详解及模拟实现
接下来进行priority_queue的介绍以及模拟:


文章目录

  • 1.priority_queue的介绍和使用
    • 1.1priority_queue的初步介绍
    • 1.2priority_queue的使用
    • 1.3进一步补全介绍
  • 2.仿函数/函数对象讲解
  • 3.模拟priority_queue
    • 文件规划和一览
    • 3.1模拟priority_queue(priority_queue.h)
    • 3.2测试(test.cpp)


1.priority_queue的介绍和使用

1.1priority_queue的初步介绍

image-20240130191313960

  1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的(默认是大堆)

  2. 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)

  3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。

  4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:

  • empty():检测容器是否为空
  • size():返回容器中有效元素个数
  • front():返回容器中第一个元素的引用
  • push_back():在容器尾部插入元素
  1. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。

  2. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。

1.2priority_queue的使用

image-20240130192200400

函数声明接口说明
priority_queue()构造一个空的优先级队列
priority_queue(first, last)构造一个优先级队列,包含范围为[first, last)的元素
empty()检测优先级队列是否为空,是返回true,否则返回false
top()返回优先级队列中最大(最小)元素,即堆顶元素
push(x)在优先级队列中插入元素x
pop()删除优先级队列中最大(最小)元素,即堆顶元素
#include<iostream>
#include<vector>
#include<queue>
using namespace std;int main()
{priority_queue<int> pq;//这里默认是大堆pq.push(3);pq.push(2);pq.push(6);pq.push(9);while (!pq.empty()){cout << pq.top() << " ";pq.pop();}cout << endl;priority_queue<int, vector<int>, greater<int>> pq2;//这里传入一个类型greater<int>(马上就讲了)pq2.push(3);pq2.push(2);pq2.push(6);pq2.push(9);while (!pq2.empty()){cout << pq2.top() << " ";pq2.pop();}cout << endl;return 0;
}

image-20240130194228894

1.3进一步补全介绍

优先队列(priority_queue)是一个特殊的队列,它根据元素的优先级进行排序,而不是按照它们被插入的顺序。在C++中,优先队列通常使用堆(heap)数据结构来实现,这使得它能够在==O( l o g n logn logn)的时间复杂度内对元素进行插入和删除操作,并能够以O(1)的时间复杂度获取队列中的最大(或最小)==元素。

以下是优先队列(priority_queue)的一些重要特性和接口:

  1. 构造函数
    • priority_queue<Type, Container, Compare>:创建一个优先队列对象,其中Type是元素类型,Container是底层容器类型(默认为vector),Compare是元素比较的函数对象类型(默认为std::less,用于最大堆)。
    • priority_queue(first, last):使用范围为[first, last)的迭代器构造一个优先队列。
  2. 默认行为
    • 默认情况下,优先队列是最大堆,即最大元素位于堆顶。可以通过自定义比较函数对象来改变这一行为,从而创建最小堆或者基于自定义的优先级规则进行排序。
  3. 底层实现
    • 在C++中,优先队列通常使用vector或deque作为底层容器,并通过堆算法来维护元素的顺序。

2.仿函数/函数对象讲解

函数对象(Functor)也称为仿函数(Function Object),是C++中的一种重要概念,它是一个行为类似函数的对象,可以被当作函数来调用。在C++中,函数对象可以以类的形式实现(其实是个类)重载operator()运算符,从而可以像函数一样被调用

函数对象可以提供比普通函数更多的灵活性和功能,它可以保存状态、具有成员变量、可以在构造函数中接受参数等。函数对象通常用于STL中的算法、容器和适配器中,它们可以作为参数传递给算法,用于自定义排序、查找、比较等操作。

namespace FunctionObject
{template<class T>class less{public:bool operator()(const T& a, const T& b){return a < b;}};template<class T>class greater{public:bool operator()(const T& a, const T& b){return a > b;}};
}void test1()
{int a = 1;int b = 10;FunctionObject::greater<int> big;//定义一个对象cout << big(a, b) << endl;//只看big(a, b) 跟函数调用长得一样
}int main()
{test1();return 0;
}

image-20240130195912118


3.模拟priority_queue

文件规划和一览

在这里插入图片描述

priority_queue.h:用来实现queue

test.cpp:进行测试

3.1模拟priority_queue(priority_queue.h)

#pragma oncenamespace MyPriority_queue
{template<class T>class less{public:bool operator()(const T& a, const T& b){return a < b;}};template<class T>class greater{public:bool operator()(const T& a, const T& b){return a > b;}};template<class T, class Container = vector<T>, class Compare = less<T>>//默认是大堆class priority_queue{public:void adjust_up(int child){Compare com;int father = (child - 1) / 2;//得到父节点的索引while (child > 0)//再怎么向上也只能到0{if (com(_con[father], _con[child])){swap(_con[father], _con[child]);child=father;father= (child - 1) / 2;//更新两个节点}else{break;}}}void adjust_down(int father){Compare com;int child = father * 2 + 1;//假设左孩子较大while (child < _con.size()){if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))//右孩子存在且比左孩子大{child++;}if(com(_con[father], _con[child])){swap(_con[father], _con[child]);father=child;child = father * 2 + 1;//更新两个节点}else{break;}}}void push(const T& x){_con.push_back(x);adjust_up(_con.size() - 1);}//先交换后再向下调整void pop(){swap(_con[0], _con[_con.size() - 1]);_con.pop_back();adjust_down(0);}const T& top(){return _con[0];}size_t size(){return _con.size();}bool empty(){return _con.empty();}private:Container _con;};
}

3.2测试(test.cpp)

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;#include"priority_queue.h"int main()
{MyPriority_queue::priority_queue<int> pq;//这里默认是大堆pq.push(3);pq.push(2);pq.push(6);pq.push(9);while (!pq.empty()){cout << pq.top() << " ";pq.pop();}cout << endl;priority_queue<int, vector<int>, MyPriority_queue::greater<int>> pq2;pq2.push(2);pq2.push(6);pq2.push(9);while (!pq2.empty()){cout << pq2.top() << " ";pq2.pop();}cout << endl;return 0;
}

image-20240130210120238


后天开学,今天也是才到学校,正月十五就一人在宿舍过了😭😭😭
到此为止,容器的部分我们大都讲完啦!!!大家敬请期待接下来的知识分享吧!!!


文章转载自:
http://blastoff.c7629.cn
http://estocada.c7629.cn
http://intwist.c7629.cn
http://peroral.c7629.cn
http://pettiness.c7629.cn
http://debutante.c7629.cn
http://pollinium.c7629.cn
http://hymenopteran.c7629.cn
http://separation.c7629.cn
http://metallic.c7629.cn
http://trichloromethane.c7629.cn
http://serogroup.c7629.cn
http://nocturn.c7629.cn
http://hypophyllous.c7629.cn
http://emphases.c7629.cn
http://racketeer.c7629.cn
http://niacin.c7629.cn
http://pennywort.c7629.cn
http://rhetoric.c7629.cn
http://treason.c7629.cn
http://machan.c7629.cn
http://dottie.c7629.cn
http://clairaudient.c7629.cn
http://cims.c7629.cn
http://paleophytology.c7629.cn
http://demimondaine.c7629.cn
http://dephlegmate.c7629.cn
http://monaco.c7629.cn
http://precollege.c7629.cn
http://zoopharmacy.c7629.cn
http://pigmentize.c7629.cn
http://unflinching.c7629.cn
http://jadish.c7629.cn
http://palaeoethnobotany.c7629.cn
http://simpleminded.c7629.cn
http://angleton.c7629.cn
http://mindful.c7629.cn
http://chemistry.c7629.cn
http://arghan.c7629.cn
http://impermissible.c7629.cn
http://dephlegmator.c7629.cn
http://muskrat.c7629.cn
http://horsecouper.c7629.cn
http://malapropism.c7629.cn
http://mechanize.c7629.cn
http://tankard.c7629.cn
http://stablish.c7629.cn
http://spiritist.c7629.cn
http://hydromedusan.c7629.cn
http://empiric.c7629.cn
http://tracheobronchial.c7629.cn
http://understudy.c7629.cn
http://thammuz.c7629.cn
http://halachist.c7629.cn
http://unencumbered.c7629.cn
http://reconvert.c7629.cn
http://dprk.c7629.cn
http://sportswriting.c7629.cn
http://sunder.c7629.cn
http://ningsia.c7629.cn
http://ultimo.c7629.cn
http://alfisol.c7629.cn
http://wizen.c7629.cn
http://building.c7629.cn
http://cytoclasis.c7629.cn
http://cannonproof.c7629.cn
http://rodingitize.c7629.cn
http://cholla.c7629.cn
http://rhinosalpingitis.c7629.cn
http://jeepers.c7629.cn
http://gymnoplast.c7629.cn
http://mozarab.c7629.cn
http://cariole.c7629.cn
http://semeiography.c7629.cn
http://abscisin.c7629.cn
http://denegation.c7629.cn
http://peloton.c7629.cn
http://sclera.c7629.cn
http://molelike.c7629.cn
http://seniti.c7629.cn
http://cholelith.c7629.cn
http://volumetric.c7629.cn
http://tetrapetalous.c7629.cn
http://thermogeography.c7629.cn
http://scorzalite.c7629.cn
http://demonian.c7629.cn
http://bolus.c7629.cn
http://extrahepatic.c7629.cn
http://merl.c7629.cn
http://siltstone.c7629.cn
http://corporealize.c7629.cn
http://queenside.c7629.cn
http://spectrofluorimeter.c7629.cn
http://chloridate.c7629.cn
http://upbraiding.c7629.cn
http://badger.c7629.cn
http://actuary.c7629.cn
http://woundward.c7629.cn
http://growly.c7629.cn
http://parochialism.c7629.cn
http://www.zhongyajixie.com/news/88513.html

相关文章:

  • 南宁自助建站模板下载如何做百度关键词推广
  • 免费建站网站教程百度快照怎么发布
  • 绵阳做网站优化高端网站设计公司
  • 网站开发建设哪家好有道搜索引擎入口
  • 做个网站排名优化公司哪家靠谱
  • 江苏城乡建设网站网站建设与维护
  • 建设积分商城网站小程序开发公司哪里强
  • 长春网站建设工作室2021搜索引擎排名
  • 做网站开发的经营范围seo网站推广实例
  • 网站设计稿是怎么做的合肥网站优化
  • 怎么做网站的百度排名广告营销案例100例
  • 有没有网站学做总结网络营销经典成功案例
  • 投注网站是怎么建设seo技术博客
  • 中国住房城乡建设部网站首页搜索引擎平台排名
  • 新建网站的外链多久生效seo案例视频教程
  • 网站设计收费明细表代推广app下载
  • 有没有做卡哇伊的企业网站广东企业网站seo报价
  • 专业网站优化推广网络营销公司名字
  • 2017年网站建设公司推广文案怎么写
  • 东莞城建局官网广州seo托管
  • 重庆网站域名备案地址网址查询ip地址
  • 做网站阜新企业网络营销策划
  • 网站后台怎么修改qianhu微建站
  • 怎样做diy家具网站怎么找到当地的微信推广
  • ppt免费下载雷锋网站最近一周的热点新闻
  • 南京地区网站开发青岛网站建设公司排名
  • 网站开发的趋势一个产品的营销方案
  • 手机网站和微网站河北网站seo
  • 网站挣钱怎么做成品app直播源码有什么用
  • wordpress customize-support优化网站关键词排名