C++ 基于x和y坐标的排序

C++ 基于x和y坐标的排序,c++,algorithm,stl,C++,Algorithm,Stl,我想根据x和y坐标对向量进行排序。下面是我所做的,但我想要的是,当我根据x排序时,我得到了正确的结果,但当我根据y进行排序时,我不希望我的x顺序发生变化 #include <vector> #include <algorithm> #include <iostream> #include <iterator> struct item_t { int x; int y; item_t( int h, int w ) : x(

我想根据
x
y
坐标对
向量进行排序。下面是我所做的,但我想要的是,当我根据
x
排序时,我得到了正确的结果,但当我根据
y
进行排序时,我不希望我的
x
顺序发生变化

#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 {
    bool operator ()(const item_t& left, const item_t& rigx) const {
        return left.x < rigx.x;
    }
};
struct compare_y {
    bool operator ()(const item_t& left, const item_t& rigx) const {
        return left.y < rigx.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::sort( items.begin(), items.end(), compare_x());
    std::copy(items.begin(),items.end(), std::ostream_iterator<item_t>(std::cout," ") );
    std::cout << std::endl;

    std::sort( items.begin(), items.end(), compare_y());
    std::copy(items.begin(),items.end(), std::ostream_iterator<item_t>(std::cout," ") );

    std::cout << std::endl;

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

friend std::ostream&operator您应该一次完成排序:

struct compare_xy {
    bool operator ()(const item_t& left, const item_t& right) const {
        return (left.x == right.x ? left.y < right.y : left.x < right.x);
    }
};
struct compare_xy{
布尔运算符()(常数项左、常数项右)常数{
返回(left.x==right.x?left.y
您只需创建一个比较器和一个调用
std::sort

struct compare_xy {
    bool operator ()(const item_t& left, const item_t& right) const {
        return (left.x < right.x) || ((left.x == right.x) && (left.y < right.y));
    }
};
struct compare_xy{
布尔运算符()(常数项左、常数项右)常数{
返回(left.x
我不太清楚你在问什么。如果你的目标是 按
y
排序,当
y
相等时,
x
确定顺序, 然后调用一个比较函数进行排序:

struct OrderYThenX
{
    bool operator()( item_t const& lhs, item_t const& rhs ) const
    {
        return lhs.y < rhs.y
            || (!(rhs.y < lhs.y) && lhs.x < rhs.x);
    }
};
struct OrderYThenX
{
布尔运算符()
{
返回左侧y<右侧y
||(!(rhs.y
这将导致
的顺序与最终在中的顺序相同 你的密码

如果,从你的部分描述和你的 例如,您希望具有相等
y
s的对象之间的顺序为 按
y
排序时保持不变,无论值是如何排列的 根据
x
订购时,应使用
std::stable\u sort
。只需
意识到它可能比
std::sort

慢,你能给出一个你期望的输出的例子吗?这个问题不是很清楚。首先你必须决定当
left.x
但是
left.y>rigx.y
时你期望什么。在这种情况下,它们应该是什么顺序?如果这是他想要的(而不是
std::stable_sort
)但是,在这种情况下,为了尊重他的描述和示例中的顺序,你应该首先比较
y
s,而不是
x
s。我所放的符合我对问题的理解-先按x排序,然后按y排序。很难确定他想从问题中得到什么。我解释了它意思是先按X排序,然后按Y排序,但当Y相等时不要扰乱顺序。事实上,他似乎在问我的是一个稳定的排序。但我承认他的问题可以用很多方式来解释。