I want to get highest co-ordinate ( x, y )
Following is the code i wrote, is this correct code.
#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;
}
Is it working for this input but not for other inputs.
Your comparison function is not a strict weak ordering, and thus using it will give undefined results.
It isn’t a strict weak ordering because the equivalence relation is not transitive. For example, (2,2) is equivalent to (4,1), and (4,1) is equivalent to (3,3). But (2,2) is not equivalent to (3,3). (Two values are considered equivalent if neither is less than the other).
You will need to provide another comparison function. Some examples:
x+yfor the two points.