Make_heap和排序x和y坐标
make_heap and sorting x and y co-ordinates
我想获得最高坐标( x, y )
以下是我写的代码,这是正确的代码吗?
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
struct item_t {
int x;
int y;
item_t( int h, int w ) : x(h), y(w) {}
friend std::ostream& operator<<(std::ostream& os, const item_t& gt) {
os << "(" << gt.x << "," << gt.y << ")";
return os;
}
};
typedef std::vector<item_t> item_list_t;
typedef item_list_t::iterator item_list_itr_t;
struct compare_x_y {
bool operator ()(const item_t& left, const item_t& right) const {
return left.x < right.x && left.y < right.y;
}
};
int main ( int argc, char **argv) {
item_list_t items;
items.push_back(item_t(15, 176));
items.push_back(item_t(65, 97));
items.push_back(item_t(72, 43));
items.push_back(item_t(102, 6));
items.push_back(item_t(191, 189));
items.push_back(item_t(90, 163));
items.push_back(item_t(44, 168));
items.push_back(item_t(39, 47));
items.push_back(item_t(123, 37));
std::make_heap (items.begin(),items.end(),compare_x_y());
std::cout << "initial max heap : " << "(" << items.front().x <<"," << items.front().y << ")" << std::endl;
}
是否对这个输入有效,而对其他输入无效
您的比较函数不是严格的弱排序,因此使用它将给出未定义的结果。
它不是严格弱排序,因为等价关系不是传递的。例如,(2,2)等于(4,1),(4,1)等于(3,3)。但是(2,2)不等于(3,3)(如果两个值都不小于另一个值,则认为是相等的)。
您需要提供另一个比较函数。一些例子:
- 比较x,如果x值相等,则比较y。
- 比较两点
x+y
值
相关文章:
- 二叉排序树无法编译
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 使用C++中的链表对与原点的距离、x 和 y 坐标进行排序
- 对坐标结构的向量进行排序
- 如何按坐标 x 或坐标 y 对 QGraphicsitem 的 QList 进行排序
- 在 c++ 中对插值的坐标进行排序和搜索
- 对坐标 c++ 的向量进行排序
- 根据点的坐标对点进行排序
- 如何按距点的距离(x1,y1)升序对一组坐标进行排序
- 对抛物线的坐标 (x,y) 进行排序 y=ax^2+bx+c x=x1,x2,x3, x4.根据 y 坐标
- 坐标的排序向量C++
- C++对指针向量进行排序,其中对象表示矩阵坐标
- 坐标的排序
- Make_heap和排序x和y坐标
- 基于 x 和 y 坐标进行排序
- 排序坐标从左上到右下
- 以蛇形图案排序点坐标
- 如何使用std::sort按x坐标对结构进行排序
- 排序屏幕坐标