C++ C++;std::push#u heap破坏堆/不';不尊重比较器
我迷上了std堆函数。。。看起来push_堆不尊重我输入的比较器 编辑:我创建了导致错误的无意义运算符=()。我修复了代码中的部分 下面是一个简单的示例:C++ C++;std::push#u heap破坏堆/不';不尊重比较器,c++,vector,c++11,heap,std,C++,Vector,C++11,Heap,Std,我迷上了std堆函数。。。看起来push_堆不尊重我输入的比较器 编辑:我创建了导致错误的无意义运算符=()。我修复了代码中的部分 下面是一个简单的示例: #include <vector> #include <iostream> #include <algorithm> using namespace std; template<class T> struct lt_entry: public binary_function <T,
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
template<class T>
struct lt_entry: public binary_function <T, T, bool>
{
bool operator()(const T &l, const T &r) const
{
cout << l._cost << " < " << r._cost << " ? " << (l._cost < r._cost) << endl;
return l._cost < r._cost;
}
};
template<typename T, class Cmp>
struct OrderedVector {
vector<T> _c;
inline void insert(T &&t) {
_c.push_back(move(t));
push_heap(_c.begin(), _c.end(), Cmp());
cout << "i is heap: " << is_heap(_c.begin(), _c.end(), Cmp()) << endl;
}
inline T extract_min() {
pop_heap(_c.begin(), _c.end(), Cmp());
T t = move(_c.back());
_c.pop_back();
cout << "e is heap: " << is_heap(_c.begin(), _c.end(), Cmp()) << endl;
return move(t);
}
inline bool empty() const {
return _c.empty();
}
};
struct Entry
{
float _cost;
Entry(Entry &&ofs) :
_cost(ofs._cost)
{
}
//This caused the error
//Entry operator=(Entry &&e) {
// return Entry(move(e));
//}
Entry& operator=(Entry &&e) {
_cost = e._cost;
return *this;
}
Entry(float cost) : _cost(cost)
{
}
};
typedef OrderedVector<Entry, lt_entry<Entry> > OrderedContainer;
int main() {
OrderedContainer h; // Doesn't work, breaks heap on second insertion
h.insert(Entry(200.1));
h.insert(Entry(100.1));
h.insert(Entry(300.1));
}
#包括
#包括
#包括
使用名称空间std;
模板
结构lt_项:公共二进制函数
{
布尔运算符()(常数T&l,常数T&r)常数
{
cout分录的(移动)赋值运算符实际上并没有修改此->成本
,或*此
中的任何内容。所有使堆
执行的交换/移动赋值都不会执行任何操作
i is heap: 1
200.1 < 100.1 ? 0
200.1 < 100.1 ? 0
i is heap: 1
200.1 < 300.1 ? 1
200.1 < 100.1 ? 0
200.1 < 300.1 ? 1
i is heap: 0 <---- Heap is broken...