C++模拟实现priority_queue(优先级队列)

一、priority_queue的函数接口

从上图我们可以看出, priority_queue也是一个容器适配器,我们使用vector容器来模拟实现priority_queue。

cpp 复制代码
namespace bit

{

  #include<vector>

  #include<functional>

  template <class T, class Container = vector<T>, class Compare = less<T> >

  class priority_queue

  {

  public:

    priority_queue();

    template <class InputIterator>

    priority_queue(InputIterator first, InputIterator last);

    bool empty() const;

    size_t size() const;

    T& top() const;

    void push(const T& x);

    void pop();

  private:

    Container c;

    Compare comp;

  };

};

我们主要实现的接口便是push、pop、top、size、empty。

在这里为什么要使用vector来模拟实现priority_queue呢,因为实现priority_queue就和我们实现堆一样,我们要实现对找到父亲和孩子节点,就必须要使用数组进行查找,所以实现priority_queue的底层是vector。

对于堆的知识可以移步:

二叉树详解_二叉树原理详解-CSDN博客

二、 模拟实现priority_queue

2.1 greater和less

cpp 复制代码
template<class T>
struct less
{
    bool operator()(const T& x, const T& y)
    {
        return x < y;
    }
};

template<class T>
struct greater
{
    bool operator()(const T& x, const T& y)
    {
        return x > y;
    }
};

我们实现greater和less就是为了实现建大堆和建小堆,我们只能够通过修改代码父子的大于小于关系才能修改建大堆还是小堆,我们实现greater和less便可以通过模板来控制建大堆还是建小堆了。

2.2 向上调整和向下调整

cpp 复制代码
void Adjustdown(size_t parent)
{
    size_t child = parent * 2 + 1;
    if (child + 1 < c.size() && comp(c[child], c[child + 1]))
    {
        child++;
    }
    while (child < c.size())
    {
        if (comp(c[parent], c[child]))
        {
            swap(c[parent], c[child]);
            parent = child;
            child = parent * 2 + 1;
        }
        else
        {
            break;
        }
    }
}

void Adjustup(size_t child)
{
    size_t parent = (child - 1) / 2;
    while (child > 0)
    {
        if (comp(c[parent], c[child]))
        {
            swap(c[parent], c[child]);
            child = parent;
            parent = (child - 1) / 2;
        }
        else
        {
            break;
        }
    }
}

向上调整和想要调整主要是为了插入和删除后使新的数组能够重新调整为大堆或小堆,这里不再详讲,不了解的可以看:二叉树详解_二叉树原理详解-CSDN博客.

2.3 模拟实现priority_queue的构造函数

cpp 复制代码
priority_queue()
{
}


template <class InputIterator>

priority_queue(InputIterator first, InputIterator last)
{
    while (first != last)
    {
        c.push_back(*first);
        ++first;
    }
    for (size_t i = c.size() - 1 - 1; i >= 0; i--)
    {
        Adjustdown(i);
    }
}

第一个无参的构造函数可以不写,因为会自动调用vector自己的构造, 而另外一个使用迭代器区间进行构造的,我们只需将每一个数据进行尾插,然后进行调整建堆即可。

2.4 模拟实现priority_queue的push

cpp 复制代码
void push(const T& x)
{
    c.push_back(x);
    Adjustup(c.size() - 1);
}

push就是将数据尾插,然后从最后一个数据开始进行向上调整,使其重新为堆。

2.5 模拟实现priority_queue的pop

cpp 复制代码
void pop()
{
    swap(c[0], c[c.size() - 1]);
    c.pop_back();
    Adjustdown(0);
}

对堆进行删除,交换头和尾的数据,并将尾的数据删掉,最后从头开始向下调整,使其重新为堆。

2.6 模拟实现priority_queue的top

cpp 复制代码
T& top() 
{
    return c[0];
}

top就是获取队列首元素的值,我们直接返回下标0的数据即可。

2.7 模拟实现priority_queue的size和empty

cpp 复制代码
bool empty() const
{
    return c.empty();
}

size_t size() const
{
    return c.size();
}

priority_queue一样使容器适配器,所以priority_queue的size和empty我们只需返回容器的size和empty即可。

2.8 检验结果

建立大堆:

建立的是大堆,每次输出后重新建堆,所以输出的是降序序列。

建立小堆:

建立的是小堆,每次输出后重新建堆,所以输出的是降序序列

检测size和empty:

push5个数据,size为5,删除一个后,size为4,empty输出0表示不为空,结果正确。

2.9 模拟实现priority_queue的完整代码

priority_queue.h文件:

cpp 复制代码
#pragma once
#include<iostream>
#include<vector>
#include<functional>
using namespace std;

namespace bit

{
    template<class T>
    struct less
    {
        bool operator()(const T& x, const T& y)
        {
            return x < y;
        }
    };

    template<class T>
    struct greater
    {
        bool operator()(const T& x, const T& y)
        {
            return x > y;
        }
    };


    template <class T, class Container = vector<T>, class Compare = less<T> >

    class priority_queue

    {

    public:

        priority_queue()
        {
        }

        void Adjustdown(size_t parent)
        {
            size_t child = parent * 2 + 1;
            if (child + 1 < c.size() && comp(c[child], c[child + 1]))
            {
                child++;
            }
            while (child < c.size())
            {
                if (comp(c[parent], c[child]))
                {
                    swap(c[parent], c[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void Adjustup(size_t child)
        {
            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (comp(c[parent], c[child]))
                {
                    swap(c[parent], c[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }

        template <class InputIterator>

        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                c.push_back(*first);
                ++first;
            }
            for (size_t i = c.size() - 1 - 1; i >= 0; i--)
            {
                Adjustdown(i);
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        T& top() 
        {
            return c[0];
        }

        void push(const T& x)
        {
            c.push_back(x);
            Adjustup(c.size() - 1);
        }

        void pop()
        {
            swap(c[0], c[c.size() - 1]);
            c.pop_back();
            Adjustdown(0);
        }

    private:

        Container c;

        Compare comp;

    };

    void test_priority_queue1()
    {
        priority_queue<int> pq;
        pq.push(1);
        pq.push(3);
        pq.push(7);
        pq.push(0);
        pq.push(9);

        while (!pq.empty())
        {
            cout << pq.top() << " ";
            pq.pop();
        }
        cout << endl;
    }

    void test_priority_queue2()
    {
        priority_queue<int, vector<int>, greater<int>> pq;
        pq.push(1);
        pq.push(3);
        pq.push(7);
        pq.push(0);
        pq.push(9);

        while (!pq.empty())
        {
            cout << pq.top() << " ";
            pq.pop();
        }
        cout << endl;
    }
    
    void test_priority_queue3()
    {
        priority_queue<int> pq;
        pq.push(1);
        pq.push(3);
        pq.push(7);
        pq.push(0);
        pq.push(9);

        cout << pq.size() << endl;
        pq.pop();
        cout << pq.size() << endl;
        cout << pq.empty() << endl;
    }
};

test.cpp文件:

cpp 复制代码
#include"priority_queue.h"

int main()
{
	//bit::test_priority_queue1();
	//bit::test_priority_queue2();
	bit::test_priority_queue3();
	return 0;
}

三、总结

以上就是模拟实现priority_queue的全部内容,希望以上所讲能够对大家有所帮助,如果对大家有帮助的话,记得一键三连哦,感谢各位。

相关推荐
磊 子3 分钟前
多态类原理+四种类型转换+异常处理
开发语言·c++·算法
脆皮炸鸡7556 分钟前
库制作与原理~动态链接
linux·开发语言·经验分享·笔记·学习方法
XMYX-07 分钟前
26 - Go recover 捕获错误:优雅恢复的真正意义
开发语言·golang
王老师青少年编程7 分钟前
csp信奥赛C++高频考点专项训练之字符串 --【回文字符串】:回文拼接
c++·字符串·csp·高频考点·信奥赛·字符串回文·回文拼接
小白学大数据7 分钟前
基于大模型的Python智能爬虫:语义识别与数据清洗实践
开发语言·爬虫·python·数据分析
迷渡27 分钟前
聊一聊 Bun 用 Rust 重写这件事
开发语言·后端·rust
古怪今人35 分钟前
Gradle构建工具 Groovy/Kotlin DSL的现代化自动化构建工具
开发语言·kotlin·自动化
赏金术士36 分钟前
Kotlin 协程与挂起函数(Coroutines & suspend)入门到实战
android·开发语言·kotlin
y = xⁿ1 小时前
Java并发八股学习日记
java·开发语言·学习
xifangge20251 小时前
【深度排障】从 OS 底层寻址剖析 javac 不是内部或外部命令 核心报错:变量空间隔离与自动化部署终极范式
java·开发语言·jdk·自动化