tuple<int, int, int>
对于下面的示例测试,这个简单的类型崩溃自定义比较器。我检查cout
了比较器中的语句cmp
,每次调用cmp
都会获得一个返回值,所以它不像元组 t1 和 t2 中的值是垃圾。
知道为什么这会崩溃吗?这个非常简单的自定义比较器有什么问题吗?
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);
}