使用std::shared_ptr的std::priority_queue派生类编译错误
compilation error with std::priority_queue derived class using std::shared_ptr
我从std::priority_queue派生来实现一些专门的方法。其中之一当我添加一个元素并且队列已满时,最小的元素将从队列中删除。
template<typename T,
typename Sequence = std::vector<T>,
typename Compare = std::less<typename Sequence::value_type> >
class fixed_priority_queue : public std::priority_queue<T, Sequence, Compare> {
friend class BCQueue_; // to access maxSize_
public:
fixed_priority_queue(unsigned int maxSize)
: maxSize_(maxSize) {}
void insertWithOverflow(const T& x) {
if (this->size() == maxSize_) {
auto beg = this->c.begin();
auto end = this->c.end();
auto min = std::min_element(beg, end);
if(x > *min) {
*min = x;
std::make_heap(beg, end);
}
}
else {
this->push(x);
}
}
// ...
private:
fixed_priority_queue() {}
const unsigned int maxSize_;
};
这是我使用的比较器:
class ScoreLessThan : public std::binary_function<
std::shared_ptr<CCandidate>, std::shared_ptr<CCandidate>, bool> {
public:
bool operator()(
const std::shared_ptr<CCandidate>& a, const std::shared_ptr<CCandidate>& b) const {
return a->score > b->score;
}
};
我包装了派生的fixed_priority_queue类,以保持功能稍微分开,所以最后我有了这个:
class BCQueue_ : public fixed_priority_queue<
std::shared_ptr<CCandidate>, std::vector<std::shared_ptr<CCandidate> >, ScoreLessThan> {
public:
BCQueue_(size_t maxSize)
: fixed_priority_queue(maxSize) {}
bool willInsert(float score) {
return size() < maxSize_ || top()->score < score;
}
};
我可以这样使用:
BCQueue_ queue(30);
CCandidate
只是一些数据的保存器。其中一个属性是score
字段,我在上面的比较器中使用它。
当我使用上面的类与CCandidate作为原始指针所有编译有问题和工作良好,现在我想用std::shared_ptr
替换原始指针(如我上面所做的),我得到一个编译错误:
... 199:5: error: no match for ‘operator>’ in ‘x >min.__gnu_cxx::__normal_iterator::operator* [with _Iterator = std::shared_ptr*, _Container = std::vector >, __gnu_cxx::__normal_iterator::reference = std::shared_ptr&]()’
...
... :199:5: note: candidates are:
... :199:5: note: operator>(int, int)
... :199:5: note: no known conversion for argument 2 from ‘std::shared_ptr’ to ‘int’
也许这是一个简单的问题。如果我正确地定义了比较器,或者如果我需要改变insertWithOverflow()
x > *min
中的比较,我不知道我应该在那里改变什么。
我应该提到的是,我已经在stackoverflow上找到了' insertWithOverflow'的实现,这正好适合我需要的。查看这里:如何使STL's priority_queue固定大小
如前所述,使用原始指针所有这些都不是问题。有人能帮我一下吗?提前感谢!您需要在函数中的任何地方使用指定的比较函数:
using std::priority_queue<T, Sequence, Compare>::comp;
void insertWithOverflow(const T& x) {
if (this->size() == maxSize_) {
auto beg = this->c.begin();
auto end = this->c.end();
auto min = std::min_element(beg, end, comp);
if(comp(*min, x)) {
*min = x;
std::make_heap(beg, end, comp);
}
}
else {
this->push(x);
}
}
相关文章:
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 使用 std::variant<...时调用 BaseState 函数而不是派生函数>
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 指向基类派生类的 std::unique_ptr 的指针
- "std::shared_from_this"不能由其派生类继承吗?
- C++派生类重载函数(带有 std::function 参数)不可见
- C++ 事件管理器的回调,使用 std::function 和 std:bind 以及派生类作为参数
- 从组件的 std::type_index 获取派生最多的类型
- std::数组派生类聚合初始化
- 如何使用派生类类型数据初始化 std::shared_ptr?
- 自定义派生的 std::exception 类的 'what' 函数返回神秘的废话
- 抽象和派生与std::list相结合
- std::用派生类对象设置,但用gcc8.1设置基类比较器
- 派生自 std::exception 的类的赋值运算符
- 在 MSVC 中,从 std::string_view 派生的对象比较不明确
- 在从仅移动类型派生的类中定义析构函数在使用 std::vector emplace_back或push_back创建时会
- 派生 std::ofstream 和重载运算符<<
- 如何将 std::list of 派生类而不是 std::list of parent class 传递给函数
- 当用作非类型模板参数时,是否可以自动派生 std::array 的大小
- 无法通过引用 std::exception 来捕获从 std::exception 派生的类