0
サイクルをグラフで検出するコードを記述しようとしています。しかし、私の再帰関数が返ってくると、その反復子はその価値を失うようです。コードは次のとおりです。イテレータが値を失う
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
vector<int > vec[100]; ///Here i hold the nodes
vector<int > ::iterator it;///Iterator for accessing "vec" elements
int viz[100],n,m;///viz remembers if a node was visited, n is the number of nodes and m the number of edges
void ham(int i)
{
for(it=vec[i].begin();it!=vec[i].end();it++)
{
int j=*it; ///j is just for probing something, no real use
if(!viz[*it])
{
viz[*it]=1; ///the it node is visited
ham(*it);///I visit this node
viz[*it]=0;///I consider this node unvisited
}
}
if(i==n)///if i equals n then I print the cycle
{
for(int j=1;j<=n;j++)
if(viz[j])
cout<<j<<" ";
cout<<endl;
return;
}
}
int main()
{
ifstream fin("graf.in");
fin>>n>>m;
for(int i=1;i<=m;i++)///reading the input and storing it in "vec"
{
int x,y;
fin>>x>>y;
vec[x].push_back(y);
vec[y].push_back(x);
}
viz[1]=1;///the first node is already visited
ham(1);///starting from the first node
return 0;
}
my関数が反復子のゲインを負の値に戻すとき。ありがとう!
質問をするのを忘れました。それは "あなたはどのようにコードをデバッグしますか"ですか?その場合は、使用しているプラットフォーム、使用しているデバッガ、スタイルの設定(ログ、シングルステップ、死後、アサーションなど)があるかどうかを教えてください。 –
デバッガを使用してコードをステップ実行する方法を学ぶ必要があるようです。良いデバッガを使用すると、プログラムを1行ずつ実行し、どこからずれているかを確認することができます。これはプログラミングをする場合に不可欠なツールです。詳しい読書:** [小さなプログラムをデバッグする方法](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver
また、あなたはどのような情報をあなたに提供していますか?プログラム?イテレータの「価値」はどういう意味ですか? –