[英]Using lower_bound function in loop is giving runtime error?
以下代碼導致運行時錯誤:在給定代碼中,``blocked''和``passed''是兩個字符串向量,長度分別為bl和pl。 “低”是字符串向量的迭代器。
int pos;
vector<string> f;
vector<string>::iterator low;
for(int i=0;i<bl;i++) {
string x=blocked[i];
low=lower_bound(passed.begin(),passed.end(),x); //lower_bound function is used on the vector 'passed'.
index=low-passed.begin(); //finding the index of the string in the vector 'passed'.
string y=passed[index];
int xlen=x.length();
int ylen=y.length();
int minlen=min(xlen,ylen);
for(int i=0;i<minlen;i++) {
if(x[i]!=y[i]) {
pos=i;
break;
}
}
string h=x.substr(0,pos+1);
f.push_back(h);
}
int d=f.size();
for(int i=0;i<f.size();i++) {
cout<<f[i]<<endl;
}
以上代碼的完整代碼位於https://ideone.com/4NgCWz 。 請幫忙!
在第57行的循環的第二次迭代中,數組的索引= 2:
$5 = std::vector of length 2, capacity 2 = {"codechef", "google"}
您應該檢查“ lower_bound()”的結果是否等於passed.end()。 如果相等,則lower_bound()返回迭代器結束,而不是最后一個元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.