C++ 排序不';好像不行

C++ 排序不';好像不行,c++,algorithm,sorting,C++,Algorithm,Sorting,这段代码是用C++编写的。我有以下结构: #include <vector> #include <algorithm> #include <iostream> using namespace std; struct Data { int h, b, w; Data(int _h, int _b, int _w) : h(_h), b(_b), w(_w) {} bool operator<(const Data&am

这段代码是用
C++
编写的。我有以下结构:

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

struct Data
{
    int h, b, w;

    Data(int _h, int _b, int _w) : h(_h), b(_b), w(_w)
    {}

    bool operator<(const Data& other) const
    {
        bool overlap = (other.b >= b && other.b <= w) ||
            (other.w >= b && other.w <= w) ||
            (other.b < b && other.w > w);
        if (overlap)
        {
            return h < other.h;
        }

        return h > other.h;
    }
};

我编写
操作符的方法是因为操作符<取决于很多数据顺序。如果我们用你的数据运行你的算法,它就是预期的输出


第一个比较是数据(1,1,2)和数据(2,3,4)之间的比较。根据您的运算符,您的
运算符Regardless:
重叠
计算起来要容易得多:
bool overlap=other.b=b(假设
b@IgorTandetnik:有关于它的指南链接吗?
这个想法是从最高-h到最低-h排序,除非有任何重叠,如果有重叠,那又怎么样?你需要为这个案例找出有意义的排序。我帮不了你,因为我不知道所有这些数字的真实世界现象是什么应该表示。
代码应该输出2 4 6 1 3 5
这是为什么?高度为5的块
H5
与高度为2的块
H2
不重叠,其高度更大,因此
H5
应该按排序顺序在
H2
之前,不是吗?你怎么会期望一个不匹配的结果你自己的解释是什么?看来你自己还没有弄清楚你到底想达到什么目的。
vector <int> getOrdering(vector <int> height, vector <int> bloom, vector <int> wilt)
{
    vector<Data> vdata;

    for (int i = 0; i < height.size(); i++)
    {
        vdata.push_back(Data(height[i], bloom[i], wilt[i]));
    }

    sort(vdata.begin(), vdata.end());
    vector<int> ans;
    for (Data data : vdata)
    {
        ans.push_back(data.h);
    }

    return ans;
}

int main()
{
    vector <int> p0 = { 1, 2, 3, 4, 5, 6 };
    vector <int> p1 = { 1, 3, 1, 3, 1, 3 };
    vector <int> p2 = { 2, 4, 2, 4, 2, 4 };

    vector<int> ans = getOrdering(p0, p1, p2);

    for (int a : ans)
    {
        cout << a << ' ';
    }
    cout << endl;

    return 0;
}
1st call: this->h = 2, other.h = 1
2nd call: this->h = 1, other.h = 2
3rd call: this->h = 3, other.h = 2
4th call: this->h = 2, other.h = 3
5th call: this->h = 4, other.h = 3
6th call: this->h = 3, other.h = 4
7th call: this->h = 5, other.h = 4
8th call: this->h = 4, other.h = 5
9th call: this->h = 6, other.h = 5
10th call: this->h = 5, other.h = 6
vector <int> p0 = { 6, 5, 4, 3, 2, 1};
vector <int> p1 = { 3, 1, 3, 1, 3, 1};
vector <int> p2 = { 4, 2, 4, 2, 4, 2};