zoukankan      html  css  js  c++  java
  • Line 923: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h)

    记录Leetcode刷题遇到的错误

    程序代码(C++):

        bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
            int rows = matrix.size();
            int cols = matrix[0].size();
            if(rows == 0 || cols == 0) return false;
            return binarySearch(matrix, target, 0, cols - 1, rows, cols);
        }

    报错代码:

    执行出错信息:  Line 923: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h)
    最后执行的输入:
    []
    0

    报错原因:

    输入为空时的判断。当rows=0的时候,数组不存在元素,也就不存在matrix[0],matrix[0]产生越界。

    程序修改:

        bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
            int rows = matrix.size();
            if(rows == 0) return false;
            int cols = matrix[0].size();
            if(cols == 0) return false;
            return binarySearch(matrix, target, 0, cols - 1, rows, cols);
        }
    ----------------------------------- 心之所向,素履所往;生如逆旅,一苇以航。 ------------------------------------------
  • 相关阅读:
    构建之法阅读笔记03
    构建之法阅读笔记02
    构建之法阅读笔记01
    人月神话阅读笔记03
    人月神话阅读笔记02
    人月神话阅读笔记01
    关于APP“跑跑”
    软件设计模式24
    软件构造9
    软件构造8
  • 原文地址:https://www.cnblogs.com/wzw0625/p/13431189.html
Copyright © 2011-2022 走看看