我是C ++的新手。我正在尝试一个leetcode问题(436。查找正确间隔)。我想通过写我自己的lower_bound来使它复杂化。但是下面的代码向我展示了runtime error: reference binding to null pointer of type 'value_type' (stl_vector.h)
。我不明白这是什么问题,请有人澄清我在做什么错。
class Solution {
public:
vector<int> findRightInterval(vector<Interval>& intervals) {
int n = intervals.size();
vector<int> res;
vector<pair<Interval, int>> s_i;
for(int i = 0; i<n; i++) {
s_i.push_back(make_pair(intervals[i], i));
}
sort(s_i.begin(), s_i.end(), [](const pair<Interval, int>& a, const pair<Interval, int> &b) -> bool {
return a.first.start < b.first.start;
});
for(auto x : s_i) {
cout<<"["<<x.first.start<<","<<x.first.end<<"],"<<x.second<<endl;
}
for(int i = 0; i<n; i++) {
int val = intervals[i].end;
cout<<val<<endl;
//pair<Interval, int> temp = make_pair(Interval(val, val), 0);
auto it = lower_bound(s_i.begin(), s_i.end(), val,
[](const pair<Interval, int> &a, int val) -> bool {
return a.first.start < val;
});
if (it != s_i.end()) {
res[i] = it->second;
} else {
res[i] = -1;
}
}
return res;
}
};