我想使用自定义类作为 boost::heap::fibonacci_heap 的类型,并且还能够迭代和修改堆的元素。我正在试验 How to orderly traverse a Boost.Heap Priority Queue and update a given element? 提供的代码.
我想出了一个工作示例,但我的 Visual Studio 2010 编译器警告我我的类 EdgeHeap 有多个复制构造函数 ( warning documentation )。
这是警告(大致):
filepath\boost\heap\fibonacci_heap.hpp(762): warning C4521: 'boost::heap::fibonacci_heap<T>': Multiple constructors
with
[
T=EdgeHeap
]
我很困惑,因为我没有声明任何复制构造函数,所以唯一的应该是编译器自动添加的那些。多个构造函数来自哪里?这也是我应该担心的事情吗?
#include <iostream>
#include <algorithm>
#include <boost/heap/fibonacci_heap.hpp>
class Edge
{
public:
int index;
double weight;
std::pair<int, int> vertices;
Edge(int i, double w, int start, int end)
{
index = i;
weight = w;
vertices.first = start;
vertices.second = end;
}
};
class EdgeHeap
{
typedef boost::heap::fibonacci_heap<EdgeHeap>::handle_type handle_t;
public:
handle_t handle;
Edge data;
EdgeHeap(const Edge &data_) : data(data_) {}
bool operator<(EdgeHeap const & rhs) const
{
return data.weight < rhs.data.weight;
}
};
void setup_handle(boost::heap::fibonacci_heap<EdgeHeap>::handle_type &&handle)
{
(*handle).handle = handle;
}
int main()
{
boost::heap::fibonacci_heap<EdgeHeap> heap;
Edge e(0, 10, 0, 1);
setup_handle(heap.push(e));
Edge e1(1, 2, 1, 2);
setup_handle(heap.push(e1));
Edge e2(2, 80, 2, 0);
setup_handle(heap.push(e2));
std::find_if(heap.ordered_begin(), heap.ordered_end(),
[&heap](const EdgeHeap &e) -> bool
{
if(e.data.index == 2)
{
const_cast<EdgeHeap &>(e).data.weight += 2;
heap.increase(e.handle);
return true;
}
return false;
});
std::for_each(heap.ordered_begin(), heap.ordered_end(),
[](const EdgeHeap &e)
{
std::cout << e.data.weight << std::endl;
});
}
最佳答案
查看 header ,boost::heap::fibonacci_heap
does have multiple copy constructors :
fibonacci_heap(fibonacci_heap const &);
fibonacci_heap(fibonacci_heap &);
(编译器消息与 EdgeHeap
或您的一种类型无关)
根据 VS documentation ,这是一个信息性警告,在这种情况下无需担心。
关于c++ - 多个复制构造函数的编译器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264191/