c++ - 运行时错误 : reference binding to null pointer of type 'std::vector<int, std::allocator<int>>' (STL_vector. h)

标签 c++ c++11 vector

这是 leetcode 210
从 0 到 n - 1,您总共需要学习 n 门类(class)。
某些类(class)可能有先决条件,例如,如果先决条件[i] = [ai, bi] 这意味着您必须在类(class) ai 之前学习类(class) bi。
给定类(class)总数 numCourses 和先决条件对列表,返回完成所有类(class)所需的类(class)顺序。
如果有很多有效答案,请返回其中任何一个。如果不可能完成所有类(class),则返回一个空数组。
输入:numCourses = 4,先决条件 = [[1,0],[2,0],[3,1],[3,2]]
输出:[0,2,1,3]
当我在下面提交此代码时,我会得到 runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h)如果有人可以帮助我,将不胜感激!

class Solution {
public:
    vector<int> adj[2002];
    bool vis[2002];
    vector <int> myvect;
    void dfs(int node)
    {
        if(!vis[node])
        {
          for(int i = 0; i < (int)adj[node].size(); i++)
          {
              if(!vis[adj[node][i]])
                  dfs(vis[adj[node][i]]);
          }
           myvect.push_back(node);
        }
        
        
    }
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
    {
       
        vector<int> adj[2002];
        int n = prerequisites.size();
        int m = prerequisites[0].size(); 
        if(n == 0 && m == 0)
        {
            for(int i = 0; i < numCourses; i++){
                myvect.push_back(i);
            }
            return myvect;
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                adj[i].push_back(j);
        }
        
        for(int i = 0; i < numCourses; i++)
            dfs(i);
        return myvect;
        
        
    }
};

最佳答案

  • 您这里只有一个小错字:return vectreturn myvect .
  • 另一个小错误在这里:int m = n ? prerequisites[0].size() : 0;
  • 修复这些错误后,该算法部分工作正常,但不适用于所有测试用例,不知道如何修复。 ( ^_^ )
  • class Solution {
        public:
            vector<int> adj[2002];
            bool vis[2002];
            vector <int> myvect;
            void dfs(int node) {
                if (!vis[node]) {
                    for (int i = 0; i < (int)adj[node].size(); i++) {
                        if (!vis[adj[node][i]]) {
                            dfs(vis[adj[node][i]]);
                        }
                    }
    
                    myvect.push_back(node);
                }
    
    
            }
            vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
    
                vector<int> adj[2002];
                int n = prerequisites.size();
                int m = n ? prerequisites[0].size() : 0;
    
                if (n == 0 && m == 0) {
                    for (int i = 0; i < numCourses; i++) {
                        myvect.push_back(i);
                    }
    
                    return myvect;
                }
    
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < m; j++) {
                        adj[i].push_back(j);
                    }
                }
    
                for (int i = 0; i < numCourses; i++) {
                    dfs(i);
                }
    
                return myvect;
    
    
            }
    };
    
  • 撇开这些不谈,我们还可以将变量命名为更具描述性:
  • // The following block might slightly improve the execution time;
    // Can be removed;
    static const auto __optimize__ = []() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(NULL);
        std::cout.tie(NULL);
        return 0;
    }();
    
    // Most of headers are already included;
    // Can be removed;
    #include <cstdint>
    #include <vector>
    
    using ValueType = std::uint_fast16_t;
    using Graph = std::vector<std::vector<ValueType>>;
    
    static const struct Solution {
            static const std::vector<int> findOrder(
                const int num_courses,
                const std::vector<std::vector<int>>& prerequisites
            ) {
                const Graph graph = buildGraph(num_courses, prerequisites);
                std::vector<ValueType> indegrees = getIndegrees(graph);
                std::vector<int> orders;
    
                for (ValueType i = 0; i < num_courses; ++i) {
                    ValueType j = 0;
    
                    for (; j < num_courses; j++) {
                        if (!indegrees[j]) {
                            orders.emplace_back(j);
                            break;
                        }
                    }
    
                    if (j == num_courses) {
                        return {};
                    }
    
                    --indegrees[j];
    
                    for (const auto& edge : graph[j]) {
                        --indegrees[edge];
                    }
                }
    
                return orders;
            }
    
        private:
            
            static const Graph buildGraph(
                const int num_courses,
                const std::vector<std::vector<int>>& prerequisites
            ) {
                Graph graph(num_courses);
    
                for (const auto& prerequisite : prerequisites) {
                    graph[prerequisite[1]].emplace_back(prerequisite[0]);
                }
    
                return graph;
            }
    
            static const std::vector<ValueType> getIndegrees(
                const Graph& graph
            ) {
                std::vector<ValueType> indegrees(std::size(graph), 0);
    
                for (const auto& adj_edges : graph) {
                    for (const auto& edge : adj_edges) {
                        ++indegrees[edge];
                    }
                }
    
                return indegrees;
            }
    
    };
    

    关于c++ - 运行时错误 : reference binding to null pointer of type 'std::vector<int, std::allocator<int>>' (STL_vector. h),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63798013/

    相关文章:

    c# - 如何使作为 SWIG shared_ptr 传递给非托管代码的托管代码对象保持事件状态?

    C++:编译器优化还是糟糕的设计?

    c++ - 是否有可能为 std::map 重载 << 运算符,其中包含对 std:string 和 std::vector<int>?

    c++ - 比较单个 vector 中的值

    c++ - 在标准 C++ 中复制 C++/CLI 多维数组模板类

    C++ 嵌套类错误 "cannot convert ... in assignment"

    C++ stoi : none of the 2 overloads could convert all the argument types

    c++ - 解决 C++ 中缺乏模板化虚函数的问题

    c++ - 如何从 ctime 巧妙地初始化 struct tm

    c++ - 通过 boost 图将 vector 变量导出到图形