좌표를 정렬 기준으로 정렬하려는 경우 x
및 y
좌표를 기준으로 정렬하고 싶습니다. 다음은 내가 한 일이지만, 내가 원하는 것은 x
을 기반으로 정렬 할 때 적절하지만, y
을 기준으로 정렬 할 때 내 x
순서를 변경하지 않으려 고합니다.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 {
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;
}
점차 증가하는 순서로 주문을 받고 싶습니다. 즉 x
및 y
이 모두 증가하고 있습니다.
예상되는 출력의 예를 들려 줄 수 있습니까? 이 질문에서 분명하지 않습니다. – Naveen
첫 번째로'left.x rigx.y' 일 때 무엇을 기대하는지 결정해야합니다. 이 경우 어떤 주문을해야합니까? –
Skyler