Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在循环中使用下界函数会导致运行时错误吗?_C++_Vector_Iterator_Binary Search_Lower Bound - Fatal编程技术网

C++ 在循环中使用下界函数会导致运行时错误吗?

C++ 在循环中使用下界函数会导致运行时错误吗?,c++,vector,iterator,binary-search,lower-bound,C++,Vector,Iterator,Binary Search,Lower Bound,以下代码导致运行时错误。在给定代码中,“blocked”和“passed”是两个字符串向量,分别具有长度bl和pllow'是字符串向量的迭代器 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

以下代码导致运行时错误。在给定代码中,“blocked”和“passed”是两个字符串向量,分别具有长度bl和pllow'是字符串向量的迭代器

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;
    }
int-pos;
向量f;
向量::迭代器低;

对于(int i=0;i在第57行循环的第二次迭代中,数组的索引为2:

$5 = std::vector of length 2, capacity 2 = {"codechef", "google"}
您应该检查“lower_bound()”的结果是否不等于passed.end()。
如果它等于,则than lower_bound()将迭代器返回到end,而不是最后一个元素。

通常,您应该检查从算法中获得的迭代器是否等于
passed.end()
,因为您不允许取消终止迭代器的限制。在这种情况下,您的
索引可能为offThnx以获取信息!。这是我第一次使用此STL,但我想知道…我将编写什么语句来避免下限()从返回迭代器到end@lazarus您无法避免它,但您可以检查它是否返回end:
if(low==pased.end()){/*处理此案例*/}否则{/*使用low*/}