在 boost::heap::p riority_queue 中推送结构对象时出错
Errors when pushing structure object in boost::heap::priority_queue
我有一个结构,其对象将被推入boost::heap::priority_queue
。
typedef struct
{
int i, j;
double deltaQ;
} heapNode;
int main()
{
double a[] = {0.03, 0.01, 0.04, 0.02, 0.05};
heapNode node;
boost::heap::priority_queue<heapNode> maxHeap;
for(int i = 0; i < 5; i++)
{
node.deltaQ = a[i];
node.i = i;
node.j = i;
maxHeap.push(node);//error
}
for(int i = 0; i < 5; i++)
{
node = maxHeap.top();
cout << node.deltaQ << " " << node.i << " " << node.j;
cout << "n";
maxHeap.pop();
}
}
此代码给出一个编译器错误,
error: no match for 'operator<' (operand types are 'const heapNode' and 'const heapNode')|
任何解决方案,我都是 usinf codeBlocks 16.01。
谢谢
您需要为heapNode
对象提供比较操作。
a( 将operator<
定义为 heapNode 的成员
struct heapNode
{
int i, j;
double deltaQ;
bool operator<(const heapNode& theOther) const {
// your comparison operation
return this->deltaQ < theOther.deltaQ;
}
};
b( 您可以将函子对象作为比较器传递给构造函数priority_queue
explicit priority_queue(value_compare const & = value_compare());
定义函子
struct cmp {
bool operator () (const heapNode& lhs, const heapNode& rhs) const {
return lhs.deltaQ < rhs.deltaQ;
}
};
将其传递给priority_queue的CTOR
cmp c;
boost::heap::priority_queue<heapNode,boost::heap::compare<cmp>> maxHeap{c};
在你的 heapNode 结构中,你需要重载运算符<</em>。
struct HeapNode
{
int i, j;
double deltaQ;
bool operator<(const HeapNode& other)
{
*return true if current HeapNode is less than other, else false*
};
} heapNode;
http://en.cppreference.com/w/cpp/language/operators 对您来说是一个很好的入门指南。
相关文章:
- 为什么我在初始化带大括号的结构时出错?
- 尝试将指针与结构一起使用时出错
- PCL 出错:体系结构x86_64 @pcl的未定义符号
- 尝试初始化结构内的联合时出错
- 当使用 std::CIN 输入结构时,它会出错
- 在 boost::heap::p riority_queue 中推送结构对象时出错
- 确定输入"conscalc: calc"的类型时出错。不能联合编码器。具有不同字段集的结构类型
- 尝试将数据添加到结构内部的矢量时出错
- 使用提升库在队列和堆栈数据结构上保存和加载数据时出错
- 尝试将结构指针传递给类时出错
- 在旧的 c++ (<c++11) 中,初始化结构会出错
- 类中私有结构的构造函数出错
- 读取结构数组时出错
- 将模板参数传递到内部结构时出错
- 创建局部结构的 std::向量时出错
- 在 C++ 中访问泛型结构的成员会出错
- 使用映射<字符串,结构>类型的映射编译头文件时出错
- 使用单元测试 (cppunit) 中另一个文件中定义的结构时出错
- 初始化作为结构成员的函数指针时出错
- C++中的初始化结构出错