mardi 27 novembre 2018

Unable to allocate a 2D vector in c++

I use dfs in a question but I have not called dfs in the main till now and my program is crashing, Recently I was programming in c and now I switched to cpp, so I am new to cpp,I know where I did mistake in vectors please tell what can I improve I know vector can increase there size automatically.

    #include<iostream>
    #include<vector>
    using namespace std;
    const int MAX = 100000;

    bool visited[MAX] = {0};
    int intime[MAX];
    int outtime[MAX];

    int timer = 0;
    void dfs(vector<vector<int>> graph, int v) 
    {
        visited[v] = true;
        timer++;
        intime[v] = timer;
        vector<int>::iterator it = graph[v].begin();
        while (it!=graph[v].end()) {
            if (visited[*it] == false)
            {
                dfs(graph, *it);
            }
            it++;
        }
        ++timer;
        outtime[v] = timer;
    }

    int main() 
    {
        vector<vector<int>> graph;
        graph[1].push_back(2);
        graph[1].push_back(3);
        graph[3].push_back(6);
        graph[2].push_back(4);
        graph[2].push_back(5);
        graph[5].push_back(7);
        graph[5].push_back(8);
        graph[5].push_back(9);  
        system("pause");
    }

Aucun commentaire:

Enregistrer un commentaire