首页 > 其他分享 >Debug Assertion Failed!:Expression: can't dereference out of range vector iterator(&&运算的注意事项)

Debug Assertion Failed!:Expression: can't dereference out of range vector iterator(&&运算的注意事项)

时间:2023-04-29 16:13:33浏览次数:37  
标签:begin end target iterator dereference Assertion mid while &&




 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 bool Find(int target, vector<int> array) {
 5     auto begin = array.begin(), end = array.end(), mid = begin + (end - begin) / 2;
 6     while ((target != *mid )&&(mid != end )) {
 7         if (target < *mid)
 8             end = mid;
 9         else
10             begin = mid + 1;
11         mid = begin + (end - begin) / 2;
12     }
13 
14     return true;
15 }
16 int main()
17 {
18     int val;
19     vector<int>array{ 2,4,7,8,11 };
20     while (cin>>val)
21     {
22         cout << Find(val, array) << endl;
23     }
24 }

 

输入12时出现以下问题:

原因是输入12最后一次while语句体内会使得mid值为5这显然指向了尾后元素(超出了容器的边界),这个时候再判断while语句会首先执行target != *mid,此时对mid解引用,因此会出现Debug,解决方法用到了&&运算的一个特性:

 

&&语句如果左边表达式为0就不会执行右边的表达式

因此将&&两边的语句调换顺序,这个时候只要mid != end 不满足,为0就不会执行target != *mid

标签:begin,end,target,iterator,dereference,Assertion,mid,while,&&
From: https://www.cnblogs.com/Sandals-little/p/17364108.html

相关文章

  • iterator:迭代器库
    #include<iterator>usingnamespacestd;//输入迭代器,用于读取容器中的元素istream_iterator<T>it(cin);//从标准输入流(cin)中读取T类型的数据//输出迭代器,用于向容器中写入元素ostream_iterator<T>it(cout,"");//将T类型的数据以空格分隔符输出到标准输出流(cout)中/......
  • java -- 异常处理、Collection、Iterator迭代器、泛型
    异常处理Java异常处理的五个关键字:try、catch、finally、throw、throws抛出异常throw在编写程序时,我们必须要考虑程序出现问题的情况当调用方法使用接受到的参数时,首先需要先对参数数据进行合法的判断,数据若不合法,就应该告诉调用者,传递合法的数据进来。这时需要使用抛出异常的......
  • foreach/增强for循环 中 使用iterator.remove();
    Set<String>set=newHashSet<>();set.add("a");set.add("b");Iterator<String>iterator=set.iterator();for(Stringstring:set){iterator.next();iterator.remove();} Excep......
  • Unable to handle kernel NULL pointer dereference at virtual address 分析
    引用:https://blog.csdn.net/agave7/article/details/119875023虽然问题不一样,但是分析问题的方法是一致的。UnabletohandlekernelNULLpointerdereferenceatvirtualaddress分析现象[136.847780]br-lan:receivedpacketoneth0.1withownaddressassourceadd......
  • python 报错AssertionError: process has already started
    python报错AssertionError:processhasalreadystarted现象  原因在Python中设置守护进程daemon,一定要放在start方法上面才会有效解决方法 ......
  • JDK源码——集合类Iterator、 Collection类
    摘要主要是讲解这个集合的原理类相关的类。参看:https://zhuanlan.zhihu.com/p/165393520这个图由Map指向Collection的Produces并不是说Map是Collection的一个子类(子接口),这里的意思是指Map的KeySet获取到的一个视图是Collection的子接口。我们可以看到集合有两个基本接口:Map和Collec......
  • JS Iterator属性
    Iterator的作用:为各种数据结构,提供一个统一的、便捷的访问接口使得数据结构的成员能够按照某种次序排列es6创造了一种新的遍历命令for...of循环,Iterator主要供for...of循环ES6规定,默认的Iterator接口部署在数据结构的Symbol.iterator属性,或者说,一个数据结构只要具有S......
  • JSON & import assertions All In One
    JSON&importassertionsAllInOneerror//constpackageInfo=require("./package.json");import*aspkgfrom"./package.json";console.log(`pkg`,pkg);......
  • IDEA Rebuild项目错误:Information:java: java.lang.AssertionError: Value of x -1
    模仿lombok工具,我的enumgen工具写完了。  公司的项目emax-rpcapi-list依赖了enumgen后,IDEARebuildProject时,或者mavenpackage/install的时候,出现报错→Information:java......
  • JMeter Assertion
     ResponseAssertion (响应断言)用于判断接口请求的响应结果是否符合预期的一种断言方式。 Applyto:MainSamplesandSubSamples: JMeterwillcheckforthe......