这是我的合并排序函数,我不明白为什么在 VC++ express 2010 中总是出现“向量下标超出范围”错误。
vector<int> Ordinatore::doMerge(vector<int> &v1, vector<int> &v2) {
int dimV1 = v1.size();
int dimV2 = v2.size();
int dimTotale = dimV1 + dimV2;
vector<int> merged;
int lh, rh;
lh=0;rh=0;
while(lh < dimV1) {
if (rh < dimV2 && v1[lh] <= v2[rh]) {
merged.push_back(v1[lh]);
lh++;
/* the trouble should start here as far as I've seen with cout & debugging */
while (rh < dimV2 && v2[rh] <= v1[lh]) {
merged.push_back(v2[rh]);
rh++;
}
}
else {
while (rh < dimV2 && v2[rh] <= v1[lh]) {
merged.push_back(v2[rh]);
rh++;
}
merged.push_back(v1[lh]);
lh++;
}
}
return merged;
}