如何使用STL PriorityQueue 优先队列

35 篇文章 2 订阅

相信大家对优先队列不陌生。

STL提供的PriorityQueue属于容器适配器,底层默认用vector容器来实现。实现原理是在用vector里构造一个

Heap(堆),堆一般是用数组来储存的。

下面是一个使用有限队列的例子,用来实现一个错误关联器,总是把优先级高的错误放在最前面。

#include <string>

#include <queue>

#include <ostream>

#include <stdexcept>

// this class is implemented as inline.

// since it is just very simple

class Error

{

public:

Error(const std::string& e, int p) : errormessage(e), priority(p) {}

std::string GetErrorMessage() const { return errormessage;}

int GetPriority() const { return priority;}

void SetErrorMessage(const std::string& s) { errormessage = s;}

void SetPriority(int p) { priority = p; }

friend bool operator<(const Error& lhs, const Error& rhs); 

friend std::ostream& operator<<(std::ostream& out, const Error& e);

private:

std::string errormessage;

int priority;

};

 

class ErrorCorrector

{

public:

ErrorCorrector() {};

// add an error to the queue

void AddError(const Error& e);

// get error

Error GetError();

bool IsEmpty() const { return mErrors.empty();}

private:

std::priority_queue<Error>  mErrors;

ErrorCorrector(const ErrorCorrector&);

ErrorCorrector& operator=(const ErrorCorrector&);

};

 

实现:

#include "PriorityError.h"

std::ostream& operator<<(std::ostream& out, const Error& e )

{

out << e.priority << ": " << e.errormessage << std::endl;

return out;

}

void ErrorCorrector::AddError( const Error& e )

{

   mErrors.push(e);

}

Error ErrorCorrector::GetError()

{

  if (mErrors.empty())

  {

    throw std::out_of_range("the error is not empty");

  }

 

  Error e = mErrors.top();

  mErrors.pop();

  return  e;

}

bool operator<( const Error& lhs, const Error& rhs )

{

  return lhs.priority < rhs.priority;

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优先队列是一种特殊的队列,可以根据元素的优先级自动排序,最常见的实现方式是使用堆(heap)。以下是一个使用堆实现优先队列的示例代码: ```c++ #include <iostream> #include <vector> using namespace std; class PriorityQueue { public: PriorityQueue() {} void push(int val) { heap.push_back(val); int idx = heap.size() - 1; while (idx > 0 && heap[idx] > heap[(idx - 1) / 2]) { swap(heap[idx], heap[(idx - 1) / 2]); idx = (idx - 1) / 2; } } void pop() { int lastIdx = heap.size() - 1; swap(heap[0], heap[lastIdx]); heap.pop_back(); int idx = 0; while (2 * idx + 1 < heap.size()) { int maxChildIdx = 2 * idx + 1; if (maxChildIdx + 1 < heap.size() && heap[maxChildIdx + 1] > heap[maxChildIdx]) { maxChildIdx++; } if (heap[idx] < heap[maxChildIdx]) { swap(heap[idx], heap[maxChildIdx]); idx = maxChildIdx; } else { break; } } } int top() { return heap[0]; } bool empty() { return heap.empty(); } private: vector<int> heap; }; ``` 在这个实现中,我们使用一个动态数组来存储队列中的元素,然后通过堆的方式来维护元素的优先级顺序。具体来说,我们使用一个大根堆来实现优先队列,每次插入元素时,我们将其插入到堆的末尾,然后不断将其与其父节点比较,如果比父节点优先级高,则交换位置,直到找到合适的位置。删除元素时,我们将堆顶元素与最后一个元素交换位置,然后弹出最后一个元素,最后不断将堆顶元素与其子节点比较,如果比子节点优先级低,则交换位置,直到找到合适的位置。获取队列首元素时,直接返回堆顶元素即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值