Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 制作_堆并排序x和y坐标_C++_Algorithm_Stl - Fatal编程技术网

C++ 制作_堆并排序x和y坐标

C++ 制作_堆并排序x和y坐标,c++,algorithm,stl,C++,Algorithm,Stl,我想获得最高坐标(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& opera

我想获得最高坐标
(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;


}
#包括
#包括
#包括
#包括
结构项{
int x;
int-y;
项目t(inth,intw):x(h),y(w){

friend std::ostream&operator您的比较函数不是a,因此使用它将给出未定义的结果

它不是严格的弱序,因为等价关系是不可传递的。例如,(2,2)等价于(4,1),(4,1)等价于(3,3)。但是(2,2)不等价于(3,3)。(如果两个值都不小于另一个,则认为这两个值是等价的)

您需要提供另一个比较函数。一些示例:

  • 比较x,然后比较y,如果x值相等
  • 比较两个点的
    x+y

您的比较函数不是,因此使用它将给出未定义的结果

它不是严格的弱序,因为等价关系是不可传递的。例如,(2,2)等价于(4,1),(4,1)等价于(3,3)。但是(2,2)不等价于(3,3)。(如果两个值都不小于另一个,则认为这两个值是等价的)

您需要提供另一个比较函数。一些示例:

  • 比较x,然后比较y,如果x值相等
  • 比较两个点的
    x+y

定义“最高”吗?字面意思是:Y坐标最大的坐标。但是比较它们的方式表明,一个坐标的X和Y都需要大于另一个坐标的X和Y。那么比较(10,1)和(9,15)呢?是的,我需要安排它们,使x和y都大于之前的x和yy。你对最高坐标的定义有点尴尬。你能添加一些例子吗?这并不能产生你期望的输出?我测试了这个,它正在工作。你说的“不用于其他输入”是什么意思?那些“其他输入”是什么?比较函数不是严格的弱排序。定义“highest”?字面意思是:具有最大Y坐标的坐标。但是比较它们的方式表明,其中一个的X和Y都需要大于另一个的X和Y。那么比较(10,1)和(9,15)呢?是的,我需要安排它们,使x和y都大于之前的x和yy。你对最高坐标的定义有点尴尬。你能添加一些例子吗?这并不能产生你期望的输出?我测试了这个,它正在工作。你说的“不用于其他输入”是什么意思?那些“其他输入”是什么?比较函数不是严格的弱排序。请修复答案中的输入错误。“transitive”->“transitive”另一个选项是比较最大值(x,y),如果相等,则比较最小值(x,y)。这可能足以解决OP的问题。请修复答案中的输入错误。“transitive”->“transitive”另一个选项是比较最大值(x,y),如果相等,则比较最小值(x,y).这可能足以解决OP的问题。