随机接入优先级队列 [英] Random access priority queue
本文介绍了随机接入优先级队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
继续列入优先队列
我正在使用随机访问实现改进的priority_queue。
I'm implementing a improved priority_queue with random access.
template <class T, class Container = std::vector<T> >
class Heap {
public:
Heap() {}
Heap(const Container& container) {
container_ = container;
std::make_heap(container_.begin(), container_.end());
}
Heap<T, Container>& operator=(const Heap<T, Container>& heap) {
if (this != &heap)
container_ = heap.container_;
return *this;
}
void push(const T& x) {
container_.push_back(x);
std::push_heap(container_.begin(), container_.end());
}
void pop() {
std::pop_heap(container_.begin(), container_.end());
container_.pop_back();
}
const T& top() {
return container_.front();
}
const Container& getContainer() const {
return container_;
}
T& operator[](size_t n) {
return container_[n];
}
typename Container::const_iterator begin() const {
return container_.begin();
}
typename Container::const_iterator end() const {
return container_.end();
}
size_t size() const {
return container_.size();
}
T& base() {
return container_.back();
}
Container::iterator erase(Container::iterator position) {
return container_.erase(position);
}
private:
Container container_;
};
我是否正确?
- 修正一元构造函数。
- 改进代码。
推荐答案
对我看起来不太好:
- 一元构造函数应该通过const引用
- 赋值运算符不检查自我分配。
-
getContainer()$ c $
- 最重要的是:为什么你想要一个随机访问优先级队列? / li>
- The unary constructor should take argument by const reference.
- The assignment operator doesn't check for self-assignment.
- The
getContainer()
method shows a lack of clarity in the interface - why would you simply expose the implementation detail like that? - Most importantly: why do you want a "random access priority queue"?
这篇关于随机接入优先级队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文