0

为 dfs 编写代码时出现以下错误:

dfs.cpp: In function ‘void dfs(int, std::vector<std::vector<int> >&)’:
dfs.cpp:92:1: error: ‘class std::stack<int>’ has no member named ‘begin’
dfs.cpp:92:1: error: ‘class std::stack<int>’ has no member named ‘end’
dfs.cpp:92:1: error: ‘class std::stack<int>’ has no member named ‘begin’
dfs.cpp:92:1: error: template argument 1 is invalid

我想迭代堆栈,当我使用迭代器以正常方式使用它时,它会给出上述错误。有人可以帮我吗。在 STL 中。

#define MAX 100
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
bool visit[MAX];
void dfs(int start,vector< vector<int>  >&v){
stack<int> S;
S.push(start);
while(S.empty()==false){
cout<<"stack:";
FIT(it,S)cout<<*it<<" ";
cout<<"\n";
int node=S.top();
S.pop();
cout<<node<<" ";
visit[node]=true;
FOR(i,0,sz(v[node])){
if(!visit[v[node][i]])S.push(v[node][i]);
}
}
return;
}

int main(){
printf("here is dfs\n");
printf("enter number of vertices\n");
int n=SI;
vector< vector<int> > v(n);
printf("enter number of edges\n");
int m=SI;
FOR(i,0,m){int a=SI,b=SI;v[a].pb(b);v[b].pb(a);}
FOR(i,0,n)if(!visit[i]){dfs(i,v);cout<<"\n";}
cout<<"\n";
    return 0;
}
4

1 回答 1

2

堆栈没有迭代器。使用向量并push_back替换pushpop_back和。popbacktop

于 2013-05-30T20:07:05.463 回答