C++ 迭代器::Begin()函数错误
我正在努力学习编程。试图用C++中的拓扑排序来解决作业调度问题。但我没有得到正确的o/p。请帮忙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
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);
}