矢量迭代器不递增
Vector iterator not being incremented
我正在尝试使用 dfs(根据 CLRS)实现拓扑排序。我确实显示了所需的输出,但程序仍在运行并导致分段错误。通过使用少量打印语句进行调试,我可以确定 for 循环永远不会退出,尽管它应该退出(当 it==Edges.end() 时)。但是,还要注意 valgrind 在
处显示错误
dfsVisit(it->first);
在 dfs() 内。我错过了什么?为什么迭代器不递增并且 for 循环退出?为什么在 valgrind 中有不同的原因?
#include<cstdio>
#include<set>
#include<list>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
struct node
{
int d, f, value;
};
std::vector< std::pair<node, node> > Edges;
std::vector< std::pair<node, node> >::iterator it;
bool *visited;
int N, myTime=0;
node node1, node2;
void dfsVisit(node);
void dfs()
{
for(it=Edges.begin(); it!=Edges.end(); it++)
if(it->first.value<N)
if(!visited[it->first.value])
dfsVisit(it->first);
}
void dfsVisit(node n)
{
myTime++; //increment myTime
n.d=myTime; //set the discovery time for node n
if(n.value<N)
if(visited[n.value])
return;
for(it=Edges.begin(); it!=Edges.end(); ++it)
{
if(it->second.value>=N)
continue;
printf("In the for loop!\n");
if(it->first.value==n.value && !visited[it->second.value])
{
printf("it->first.value: %d\n",it->first.value+1);
printf("it->second.value: %d\n",it->second.value+1);
dfsVisit(it->second);
printf("Inside for and if\n");
}
printf("Inside for but outside if!\n");
printf("Edges.end()-it: %d\n",Edges.end()-it);
}
visited[n.value]=true;
myTime++;
n.f=myTime;
printf("For node %d, discovery time and finishing time is: %d, %d", n.value, n.d, n.f);
return;
}
int main()
{
int M, firstOfRule, secondOfRule, data, i;
//node node1, node2;
scanf("%d""%d",&N,&M);
visited=new bool[N];
for(i=0;i<N;i++)
visited[i]=false;
while(M--)
{
scanf("%d",&firstOfRule);
scanf("%d",&secondOfRule);
while(secondOfRule--)
{
scanf("%d",&data);
node1.value=firstOfRule-1;
node2.value=data-1;
Edges.push_back(std::make_pair(node1,node2));
printf("Pair: %d,%d\n", node1.value+1, node2.value+1);
}
}
for(std::vector< std::pair<node, node> >::const_iterator it=Edges.begin(); it!=Edges.end(); ++it)
printf("Connected %d and %d\n",it->first.value+1,it->second.value+1);
dfs();
return 0;
}
输出文件如下:
Pair: 1,2
Pair: 2,3
Connected 1 and 2
Connected 2 and 3
In the for loop!
it->first.value: 1
it->second.value: 2
In the for loop!
Inside for but outside if!
Edges.end()-it: 2
In the for loop!
it->first.value: 2
it->second.value: 3
In the for loop!
Inside for but outside if!
Edges.end()-it: 2
In the for loop!
Inside for but outside if!
Edges.end()-it: 1
For node 2, discovery time and finishing time is: 3, 4Inside for and if
Inside for but outside if!
Edges.end()-it: 0 //-----> Why doesn't it exit here?
In the for loop!
Inside for but outside if!
Edges.end()-it: -1
In the for loop!
Inside for but outside if!
Edges.end()-it: -2
In the for loop!
Inside for but outside if!
Edges.end()-it: -3
... and so on until the program crashes!
感谢您的帮助!
您已将 std::vector< std::pair<node, node> >::iterator it;
声明为全局变量。
但是你在递归函数中使用它dfsVisit
。这意味着当对 dfsVisit
的嵌套调用结束时,它会离开 it == Edges.end()
。但是之前的 dfsVisit
继续执行它的循环,做 ++it
导致未定义的行为。
要解决此问题,请将 it
设为 dfsVisit
函数的局部变量。
注意: 如果您的编译器支持 C++11,您可以避免一些输入并使用 auto
声明迭代器。
我正在尝试使用 dfs(根据 CLRS)实现拓扑排序。我确实显示了所需的输出,但程序仍在运行并导致分段错误。通过使用少量打印语句进行调试,我可以确定 for 循环永远不会退出,尽管它应该退出(当 it==Edges.end() 时)。但是,还要注意 valgrind 在
处显示错误dfsVisit(it->first);
在 dfs() 内。我错过了什么?为什么迭代器不递增并且 for 循环退出?为什么在 valgrind 中有不同的原因?
#include<cstdio>
#include<set>
#include<list>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
struct node
{
int d, f, value;
};
std::vector< std::pair<node, node> > Edges;
std::vector< std::pair<node, node> >::iterator it;
bool *visited;
int N, myTime=0;
node node1, node2;
void dfsVisit(node);
void dfs()
{
for(it=Edges.begin(); it!=Edges.end(); it++)
if(it->first.value<N)
if(!visited[it->first.value])
dfsVisit(it->first);
}
void dfsVisit(node n)
{
myTime++; //increment myTime
n.d=myTime; //set the discovery time for node n
if(n.value<N)
if(visited[n.value])
return;
for(it=Edges.begin(); it!=Edges.end(); ++it)
{
if(it->second.value>=N)
continue;
printf("In the for loop!\n");
if(it->first.value==n.value && !visited[it->second.value])
{
printf("it->first.value: %d\n",it->first.value+1);
printf("it->second.value: %d\n",it->second.value+1);
dfsVisit(it->second);
printf("Inside for and if\n");
}
printf("Inside for but outside if!\n");
printf("Edges.end()-it: %d\n",Edges.end()-it);
}
visited[n.value]=true;
myTime++;
n.f=myTime;
printf("For node %d, discovery time and finishing time is: %d, %d", n.value, n.d, n.f);
return;
}
int main()
{
int M, firstOfRule, secondOfRule, data, i;
//node node1, node2;
scanf("%d""%d",&N,&M);
visited=new bool[N];
for(i=0;i<N;i++)
visited[i]=false;
while(M--)
{
scanf("%d",&firstOfRule);
scanf("%d",&secondOfRule);
while(secondOfRule--)
{
scanf("%d",&data);
node1.value=firstOfRule-1;
node2.value=data-1;
Edges.push_back(std::make_pair(node1,node2));
printf("Pair: %d,%d\n", node1.value+1, node2.value+1);
}
}
for(std::vector< std::pair<node, node> >::const_iterator it=Edges.begin(); it!=Edges.end(); ++it)
printf("Connected %d and %d\n",it->first.value+1,it->second.value+1);
dfs();
return 0;
}
输出文件如下:
Pair: 1,2
Pair: 2,3
Connected 1 and 2
Connected 2 and 3
In the for loop!
it->first.value: 1
it->second.value: 2
In the for loop!
Inside for but outside if!
Edges.end()-it: 2
In the for loop!
it->first.value: 2
it->second.value: 3
In the for loop!
Inside for but outside if!
Edges.end()-it: 2
In the for loop!
Inside for but outside if!
Edges.end()-it: 1
For node 2, discovery time and finishing time is: 3, 4Inside for and if
Inside for but outside if!
Edges.end()-it: 0 //-----> Why doesn't it exit here?
In the for loop!
Inside for but outside if!
Edges.end()-it: -1
In the for loop!
Inside for but outside if!
Edges.end()-it: -2
In the for loop!
Inside for but outside if!
Edges.end()-it: -3
... and so on until the program crashes!
感谢您的帮助!
您已将 std::vector< std::pair<node, node> >::iterator it;
声明为全局变量。
但是你在递归函数中使用它dfsVisit
。这意味着当对 dfsVisit
的嵌套调用结束时,它会离开 it == Edges.end()
。但是之前的 dfsVisit
继续执行它的循环,做 ++it
导致未定义的行为。
要解决此问题,请将 it
设为 dfsVisit
函数的局部变量。
注意: 如果您的编译器支持 C++11,您可以避免一些输入并使用 auto
声明迭代器。