当有这样的区间【0,4】,【6,12】,【5,9】,【7,10】, 区间之间有交叉,即交集。要求求出没有交集的区间集合,包含以上所有的区间范围。
结果如: 【0,4】【5,6】【6,7】【7,9】 【9,10】 【10,12】
void Get_unoverlap_interval(const vector<pair<int ,int>> & sinterval, vector<pair<int ,int>> & sre){
vector<pair<int ,int>> svec;
//vector<pair<int,int>> sre;
int overlap = 0;
vector<pair<int,int>>::const_iterator iter = sinterval.begin();
for(; iter != sinterval.end(); iter++){
svec.push_back(make_pair(iter->first,0));
svec.push_back(make_pair(iter->second,1));
}
sort(svec.begin(),svec.end());
for(iter = svec.begin(); iter != svec.end(); iter++){
if(iter->second == 0){
overlap ++;
}
else{
overlap --;
}
if(overlap > 0){
sre.push_back(make_pair(iter->first,(iter+1)->first));
}
}
//return sre;
}