Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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/8/sorting/2.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++_Sorting - Fatal编程技术网

C++ 通过增加x坐标和y坐标对点进行排序

C++ 通过增加x坐标和y坐标对点进行排序,c++,sorting,C++,Sorting,我搞不懂什么是通过增加x坐标和y坐标来对点的结构进行排序? 例如,如果我们有 struct point { int x,y; } 当然,我们必须创建我们的排序函数 bool compare(const point &a,const point &b) { //here i think we have to use this comparison method if(a.x!=b.x) return (a.x>b.x); else return (a.y>b

我搞不懂什么是通过增加x坐标和y坐标来对点的结构进行排序? 例如,如果我们有

struct point
{
int x,y;
}
当然,我们必须创建我们的排序函数

bool compare(const point &a,const point &b)
{

//here i think we have to use this comparison method
  if(a.x!=b.x)
 return (a.x>b.x);
else
 return (a.y>b.y);


}
最后我们会有

vector<point>points;
sort(points.begin(),points.end(),compare);
矢量点;
排序(points.begin()、points.end()、compare);

我比较感兴趣的是,如果我的比较方法是正确的,我的意思是增加x坐标和y坐标,这是我的比较方法所做的吗?

< P> > C++代码库中的所有<代码>排序> <代码>方法假设,如果左边是“小于”右边,则正在通过的比较方法返回<>代码>真/代码>。因此,您的
compare
方法可能基本上与您希望的相反


<>也要考虑编写<代码>运算符> p>这意味着你使用x坐标来排序点,如果x坐标相等,则使用y坐标。诸如此类(未经测试):

bool比较(常数点a、常数点b)
{
如果(a.xb.x)为其他情况
{
返回false;
}
否则如果(a.y
我不知道增加x和y坐标意味着什么样的排序,你也不知道增加x和y坐标意味着什么样的排序,所以谁能说任何代码是否正确?这个短语没有任何明显的含义,所以我认为你应该问让你这么做的人他们的意思是什么。先按x坐标排序,然后按y排序coordinates@dato如果你确定这就是你的意思,那么你的代码看起来没问题,除了它是按降序排序,而不是按升序排序。
bool compare(const point &a,const point &b)
{
  if (a.x < b.x)
  {
    return true;
  }
  else if (a.x > b.x)
  {
    return false;
  }
  else if (a.y < b.y)
  {
    return true;
  }

  return false;
}