使用自定义>比较器进行 STD 堆排序
std heap sorting using custom > comparator
我正在尝试使用std堆对整数数组进行排序。
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
struct compare {
bool operator() (const int& s1, const int& s2) const{
if (s1 < s2) return true;
return false;
}
};
int main() {
vector<int> v;
v.push_back(8);
v.push_back(1);
v.push_back(3);
v.push_back(2);
v.push_back(4);
make_heap(v.begin(), v.end(), compare());
for (int i=0; i<5; i++) {
int t = v.front();
pop_heap(v.begin(), v.end()); v.pop_back();
cout << t << ' ';
}
cout << endl;
}
现在这个排序输出8,4,3,2,1,是正确的。
但是如果我改变s1 <在比较器函数中S2到s1> S2,结果变为1,4,8,3,2。在比较器函数中S2到s1>
我在这里做错了什么?
啊,我想起来了,我必须在pop_heap中也包含这个比较器
相关文章:
- 无法在我的堆排序代码中找到错误.无法正确执行.C++
- 如何使用用户定义的键控制和修改 std::map 排序
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- 堆排序它不起作用或计算不正确
- 试图做堆排序,但被卡住了
- treap 中的轮换会违反它的堆排序或二叉搜索树顺序吗?
- 计算在堆排序中完成的比较次数
- 堆排序和插入排序
- 为什么tha std :: list ::排序不起作用
- 堆排序未产生正确的输出
- 结构的堆排序
- C++堆排序引发错误
- 将多维std::vector排序为单个向量
- 堆排序CPU时间
- 提高循环缓冲区或堆排序性能
- C++堆排序算法在4100多个数据条目后崩溃
- std::将排序谓词映射到按字符串大小排序,但保留不同的字符串
- 为什么我的堆排序比 Java 和 C++ 的排序函数快
- 堆排序 - 实现的复杂性
- 使用自定义>比较器进行 STD 堆排序