priority_queue 模拟实现

简介: priority_queue 模拟实现

priority_queue(优先队列)是一种特殊的队列数据结构,它的每个元素都有一个与之关联的优先级。在优先队列中,元素按照优先级的顺序进行排列,具有较高优先级的元素排在前面,较低优先级的元素排在后面。

C++标准库中的priority_queue:

priority_queue

下面是riority_queue的简单模拟实现:

#pragma once
#include <vector>
#include <iostream>
using namespace std;
namespace hsl
{
  template<class T, class Container = vector<T>,class Compar = Less<T>>
  class priority_queue
  {
  public:
    priority_queue()
    {}
    template <class InputIterator>
    priority_queue(InputIterator first, InputIterator last)
      :_con(first, last)
    {
      for (int i = (_con.size() - 2) / 2; i >= 0; i--)
      {
        adjust_down();
      }
    }
    void adjust_up(int child)
    {
      Compar com;
      int 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 adjust_down(size_t parent)
    {
      Compar com;
      size_t child = parent * 2 + 1;
      while (child < _con.size())
      {
        if (child + 1 < _con.size() && com(_con[child] , _con[child + 1]))
        {
          ++child;
        }
        if (com(_con[parent] , _con[child]))
        {
          swap(_con[parent], _con[child]);
          parent = child;
          child = parent * 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];
    }
    bool empty()
    {
      return _con.empty();
    }
    size_t size()
    {
      return _con.size();
    }
  private:
    Container _con;
  };
  template<class T>
  class Less
  {
  public:
    const T& operator()(const T& a, const T& b)
    {
      return a < b;
    }
  };
  template<class T>
  class Greater
  {
  public:
    const T& operator()(const T& a, const T& b)
    {
      return a > b;
    }
  };
}
相关文章
|
6月前
|
Python
双端优先级队列(Double-Ended Priority Queue
双端优先级队列(Double-Ended Priority Queue)是一种支持在两端进行插入和删除操作的优先级队列。它可以在 O(log n) 的时间复杂度内完成插入、删除、查询操作。双端优先级队列可以使用二叉堆或线段树等数据结构实现。
133 6
|
5月前
|
算法 编译器 C++
priority_queue简单实现(优先级队列)(c++)
priority_queue介绍 pri_que是一个容器适配器,它的底层是其他容器,并由这些容器再封装而来。类似于heap的结构。默认为大堆。
36 0
|
3月前
|
算法 C语言 C++
C++:priority_queue模拟实现
C++:priority_queue模拟实现
22 0
|
4月前
|
存储 算法 C++
priority_queue的模拟实现
priority_queue的模拟实现
40 0
|
4月前
|
测试技术 C++
c++优先队列priority_queue(自定义比较函数)
c++优先队列priority_queue(自定义比较函数)
81 0
|
5月前
|
存储 算法 C++
C++优先级队列priority_queue详解及其模拟实现
C++优先级队列priority_queue详解及其模拟实现
50 0
|
6月前
|
存储 算法 C++
C++ priority_queue
C++ priority_queue
|
7月前
|
算法 C++ 容器
【C++】priority_queue使用和模拟实现——仿函数(下)
【C++】priority_queue使用和模拟实现——仿函数(下)
【C++】priority_queue使用和模拟实现——仿函数(下)
|
7月前
|
C++ 容器
【C++】priority_queue使用和模拟实现——仿函数(上)
【C++】priority_queue使用和模拟实现——仿函数(上)
|
8月前
|
设计模式 算法 C语言
C++实践模拟(stack,queue & priority_queue,仿函数)
C++实践模拟(stack,queue & priority_queue,仿函数)
39 0