Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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++ 迭代器::Begin()函数错误_C++ - Fatal编程技术网

C++ 迭代器::Begin()函数错误

C++ 迭代器::Begin()函数错误,c++,C++,我正在努力学习编程。试图用C++中的拓扑排序来解决作业调度问题。但我没有得到正确的o/p。请帮忙 Code: #include<iostream> #include<conio.h> #include<stack> #include<list> using namespace std; int V,E; list<int> *adj; void topologicalSort(int v,bool visited[],stack&l

我正在努力学习编程。试图用C++中的拓扑排序来解决作业调度问题。但我没有得到正确的o/p。请帮忙

Code:

#include<iostream>
#include<conio.h>
#include<stack>
#include<list>
using namespace std;
int V,E;
list<int> *adj;

void topologicalSort(int v,bool visited[],stack<int> &s)
{
    visited[v] = true;

    list<int>::iterator i;
    for(i = adj[v].begin();i != adj[v].end();++i)// Iterating through the adj list for all nodes
        if(!visited[*i])// If node not visited call again
            topologicalSort(*i,visited,s);
    s.push(v);
}

void main()
{
    int T_case,u,v;
    bool *visited;
    stack<int> s;
    cin>>T_case;
    for(int T = 1; T<=T_case; T++)
    {
        cin>>V>>E;
        adj = new list<int>[V];
        visited = new bool[V];
        for(int e = 1; e <= E; e++)
        {
            cin>>u>>v;
            adj[u].push_back(v);
        }

        for(int i=1;i<=V;i++)
            visited[i] = false;


        for(int i=1;i<=V;i++)
        {
            if(visited[i] == false)
                topologicalSort(i,visited,s);// Recurvivelly calling Topological sort for all nodes.
        }
        //cout<<s.size()<<endl;
        while(s.empty() == false)
        {
            cout<<s.top()<<" ";
            s.pop();
        }

    }
    getch();
    return;
}
试图处理节点5时,我的程序在第
行中断(i=adj[v].begin();i!=adj[v].end();++i)
。 我不清楚为什么
Iterator.begin()
函数正在转储

我真的很感激任何帮助。
谢谢。

< P> C++语言中的数组是从<代码> 0 < /COD>索引的。如果您使用
T*p=new T[N]
创建了一个数组,则允许您从
p[0]
p[N-1]
访问元素。不允许您访问
p[N]
。它不存在

同时,在代码中,您似乎假设数组的索引从
1
N
。例如,这在以下周期中是显而易见的

    for(int i=1;i<=V;i++)
        visited[i] = false;

    for(int i=1;i<=V;i++)
    {
        if(visited[i] == false)
            topologicalSort(i,visited,s);
    }

for(int i=1;从
0

    for(int i=1;i<=V;i++)
        visited[i] = false;

    for(int i=1;i<=V;i++)
    {
        if(visited[i] == false)
            topologicalSort(i,visited,s);
    }