中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

昆明網(wǎng)站建設公司_百度推廣怎么提高關鍵詞排名

昆明網(wǎng)站建設公司_,百度推廣怎么提高關鍵詞排名,東莞網(wǎng)站建設都找菲凡網(wǎng)絡,建站設計文章目錄一、priority_queue類簡介二、priority_queue類常用接口三、priority_queue類的使用四、STL中priority_queue類的模擬實現(xiàn)一、priority_queue類簡介 優(yōu)先隊列是一種容器適配器,根據(jù)嚴格的弱排序標準,它的第一個元素總是它所包含的元素中最大的?!?article class="baidu_pl">

文章目錄

  • 一、priority_queue類簡介
  • 二、priority_queue類常用接口
  • 三、priority_queue類的使用
  • 四、STL中priority_queue類的模擬實現(xiàn)

一、priority_queue類簡介

  1. 優(yōu)先隊列是一種容器適配器,根據(jù)嚴格的弱排序標準,它的第一個元素總是它所包含的元素中最大的
  2. 類似于堆,在堆中可以隨時插入元素,并且只能檢索最大堆元素(優(yōu)先隊列中位于頂部的元素)。
  3. 優(yōu)先隊列被實現(xiàn)為容器適配器,容器適配器即將特定容器類封裝作為其底層容器類。元素從特定容器的“尾部”彈出,其稱為優(yōu)先隊列的頂部。
  4. 底層容器可以是任何標準容器類模板,也可以是其他特定設計的容器類。容器應該可以通過隨機訪問迭代器訪問,并支持以下操作:empty() 檢測容器是否為空size():返回容器中有效元素個數(shù)front():返回容器中第一個元素的引用push_back():在容器尾部插入元素pop_back():刪除容器尾部元素。
  5. 標準容器類vectordeque滿足這些需求。默認情況下,如果沒有為特定的priority_queue類實例化指定容器類,則使用vector。
  6. 需要支持隨機訪問迭代器,以便始終在內部保持堆結構。容器適配器通過在需要時自動調用算法函數(shù)make_heap、push_heap和pop_heap來自動完成此操作。

??以上內容來自:www.cplusplus.com priority_queue類文檔的介紹,在STL的學習中,推薦大家看這個文檔,對每個類都有詳細的介紹。


總結

??優(yōu)先級隊列 (priority_queue) 屬于STL中的容器適配器,其底層存儲數(shù)據(jù)的容器默認使用vector,并且使用堆算法使得vector中的元素構造成堆的結構。所以,可以說優(yōu)先級隊列 (priority_queue) 就是堆,默認情況下是大堆。在使用時,與queue相同,要引入頭文件#include<queue>。


二、priority_queue類常用接口

函數(shù)名稱功能說明
priority_queue()構造一個空的優(yōu)先級隊列
priority_queue(first,last)構造一個空的優(yōu)先級隊列
empty()檢測優(yōu)先級隊列是否為空,是返回true,否則返回false
top( )返回優(yōu)先級隊列中最大/最小元素,即堆頂元素
push(X)在優(yōu)先級隊列中插入元素X
pop()刪除優(yōu)先級隊列中最大/最小元素,即堆頂元素

priority_queue類常用接口應用

#include<iostream>
#include<queue>
#include<vector>
#include<functional> 
using namespace std;int main()
{vector<int> v = { 10,60,50,20 };//priority_queue<int> pq;priority_queue<int> pq(v.begin(),v.end());pq.push(30);while (!pq.empty()){cout<< pq.top()<<" ";pq.pop();}return 0;
}

三、priority_queue類的使用

切換大小堆

?? 默認情況下,priority_queue是創(chuàng)建的是大堆,其底層按照小于號比較,默認模板參數(shù)為template<class T, class Container =std::vector<T>, class Compare = std::less<T>>;如果要創(chuàng)建小堆,將第三個模板參數(shù)換成greater即可,例如priority_queue<int, vector<int>, greater<int>> pq2(v.begin(), v.end());


greater<int>less<T>

??greater和less的頭文件為#include <functional>,具體實現(xiàn)見第三節(jié)模擬實現(xiàn)部分。 <functional>是C++標準庫中的一個頭文件,定義了C++標準中多個用于表示函數(shù)對象的類模板,包括算法操作、比較操作、邏輯操作;

??建堆時,less<T>是大頂堆,堆元素是從大到小;greater<T>是小頂堆,堆元素是從小到大。

??排序時,less<T>是升序,數(shù)組元素是從小到大;greater<T>是降序堆,數(shù)組元素是從大到小。


如果priority_queue中放自定義類型的數(shù)據(jù),需要在自定義類型中提供> 或者< 的重載

class Date
{
public:Date(int year = 2023, int month = 1, int day = 1): _year(year), _month(month), _day(day){}bool operator<(const Date& d)const{return (_year < d._year) ||(_year == d._year && _month < d._month) ||(_year == d._year && _month == d._month && _day < d._day);}bool operator>(const Date& d)const{return (_year > d._year) ||(_year == d._year && _month > d._month) ||(_year == d._year && _month == d._month && _day > d._day);}friend ostream& operator<<(ostream& _cout, const Date& d){_cout << d._year << "-" << d._month << "-" << d._day;return _cout;}
private:int _year;int _month;int _day;
};void TestPriorityQueue()
{// 大堆,需要用戶在自定義類型中提供<的重載priority_queue<Date> q1;q1.push(Date(2023, 1, 2));q1.push(Date(2023, 1, 3));q1.push(Date(2023, 1, 4));cout << q1.top() << endl;// 如果要創(chuàng)建小堆,需要用戶提供>的重載priority_queue<Date, vector<Date>, greater<Date>> q2;q2.push(Date(2023, 1, 2));q2.push(Date(2023, 1, 3));q2.push(Date(2023, 1, 4));cout << q2.top() << endl;
}

四、STL中priority_queue類的模擬實現(xiàn)

#pragma once
#include<vector>namespace MyPriority_queue
{template<class T>struct less{bool operator()(const T& l, const T& r){return l < r;}};template<class T>struct greater{bool operator()(const T& l, const T& r){return l > r;}};template<class T, class Container =std::vector<T>, class Compare = std::less<T>>class priority_queue{public://typedef typename Container::value_type VT;void AdjustUp(size_t child){Compare com;size_t parent = (child - 1) / 2;while (child > 0){//if (_con[parent] > _con[child])if (com(_con[parent], _con[child])){swap(_con[parent], _con[child]);child = parent;parent = (child - 1) / 2;}else{break;}}}void push(const T& x){_con.push_back(x);AdjustUp(_con.size() - 1);}void AdjustDwon(size_t parent){Compare com;size_t child = parent * 2 + 1;while (child < _con.size()){//if (child+1 < _con.size() && _con[child] > _con[child+1])if (child + 1 < _con.size() && com(_con[child], _con[child + 1])){++child;}//if (_con[parent] > _con[child])if (com(_con[parent], _con[child])){swap(_con[parent], _con[child]);parent = child;child = parent * 2 + 1;}else{break;}}}void pop(){swap(_con[0], _con[_con.size() - 1]);_con.pop_back();AdjustDwon(0);}T top(){return _con[0];}size_t size(){return _con.size();}bool empty(){return _con.empty();}private:Container _con;};
}
http://m.risenshineclean.com/news/62144.html

相關文章:

  • 寧波專門做網(wǎng)站武漢大學人民醫(yī)院精神科
  • 宣城市建設監(jiān)督管理局網(wǎng)站下載企業(yè)宣傳網(wǎng)站
  • 婚慶網(wǎng)站源碼網(wǎng)絡銷售推廣公司
  • 軟件商城官網(wǎng)廈門seo
  • 武漢網(wǎng)站開發(fā)招聘如何推廣網(wǎng)頁
  • 建行官方網(wǎng)站優(yōu)化大師是什么軟件
  • ps怎么做網(wǎng)站橫幅廣告長沙h5網(wǎng)站建設
  • 做網(wǎng)站網(wǎng)頁建站企業(yè)網(wǎng)站
  • 沈陽做網(wǎng)站怎樣收費百度商業(yè)平臺官網(wǎng)
  • 醫(yī)院網(wǎng)站建設思路上海搜索引擎優(yōu)化公司
  • 營銷型網(wǎng)站建設 價格軟文案例400字
  • 房山區(qū)網(wǎng)站建設做網(wǎng)站
  • 美妝網(wǎng)站模版360優(yōu)化大師舊版本
  • 網(wǎng)站開發(fā)廈門廣告營銷案例100例
  • 網(wǎng)站開發(fā)什么語言友情鏈接可以隨便找鏈接加嗎
  • 宣城網(wǎng)站建設寧波seo網(wǎng)站服務
  • 做網(wǎng)站排名公司推薦網(wǎng)絡營銷方案例文
  • 福州便民網(wǎng)免費發(fā)布信息seo文章優(yōu)化技巧
  • 中企動力科技股份有限公司做網(wǎng)站網(wǎng)絡銷售是干嘛的
  • 西安網(wǎng)站建設維護如何申請一個網(wǎng)站域名
  • 有哪些幫別人做任務賺錢的網(wǎng)站網(wǎng)絡推廣員每天的工作是什么
  • 衡水做wap網(wǎng)站建設廣州網(wǎng)站優(yōu)化價格
  • 上海建網(wǎng)站開發(fā)公seo計費系統(tǒng)開發(fā)
  • 外包是什么意思石家莊seo網(wǎng)站排名
  • 網(wǎng)站做排名2015新年桂林seo
  • centos搭建wordpressseo崗位是什么意思
  • 佛山房地產網(wǎng)站建設企業(yè)網(wǎng)址
  • 做娛樂網(wǎng)站彩票代理重慶森林影評
  • 自主做網(wǎng)站東莞seo技術培訓
  • 通用網(wǎng)站建設如何做好精準營銷