This simple custom-comparator for type tuple<int, int, int>
crashes for the example test below. I checked with the cout
statements in the cmp
comparator that each call to cmp
gets a return value, so it's not like the values in the tuples t1 and t2 are junk.
Any idea why this is crashing? Is there anything wrong with this very simple custom-comparator?
class Solution {
public:
vector<int> assignBikes(vector<vector<int>>& ws, vector<vector<int>>& bs) {
int n = ws.size(), m = bs.size();
vector<tuple<int, int, int>> dist;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int d = abs(ws[i][0]-bs[j][0]) + abs(ws[i][1]-bs[j][1]);
dist.push_back(make_tuple(d, i, j));
}
}
sort(dist.begin(), dist.end(), cmp());
}
struct cmp {
bool operator() (tuple<int, int, int>& t1, tuple<int, int, int>& t2) {
int d1 = get<0>(t1), d2 = get<0>(t2), w1 = get<1>(t1), w2 = get<1>(t2), b1 = get<2>(t1), b2 = get<2>(t2);
cout << d1 << " " << w1 << " " << b1 << " " << d2 << " " << w2 << " " << b2 ;
bool ret = false;
if (d1 < d2) ret = true;
else if (w1 < w2) ret = true;
else if (b1 < b2) ret = true;
cout << " " << ret << " " << endl;
return ret;
}
};
};
int main() {
Solution s;
vector<vector<int>> ws = {{0,0},{1,0},{2,0},{3,0},{6,0}};
vector<vector<int>> bs = {{0,999},{1,999},{2,999},{3,0},{6,0}};
s.assignBikes(ws, bs);
}