如何将set <int> var传递给函数?

时间:2016-09-13 09:14:26

标签: c++11 stl depth-first-search

该程序中的函数dfs()对集合A&amp; A进行操作。当集合A和int C [MAX]被声明为全局时,程序工作正常。但是当我尝试使用这个程序t次时,集合A和C用以前的测试用例的值初始化,这会产生错误的输出。如何让这个程序接受A&amp; A中的新值每个下一个案例都有C.注意:此程序用于查找具有n个节点和m个边数的图是否为二分图。

    #include <bits/stdc++.h>
   using namespace std;

  const int MAX=1000000; // maximum number of vertices

   int dfs(int x,const set<int>& A,int C[]){
for(int y:A[x]){
    if(C[y]==C[x])return 0;// this means the graph is not bipartite
    if(C[y]==0){
        if(C[x]==1) C[y]=2;
        else C[y]=1;
        dfs(y,A,C);
        return 1;
    }
}
   }

int main(){

int t;
scanf("%d",&t);
while(t--)
{
    set<int> A[MAX];// Here, i declare set<int> A and Int C[MAX] in local          scope
    int C[MAX];

    // Passing set<int> A and int C[] to dfs()..

    int res = dfs(i,A,int C);    
 }

如果我将我的代码更改为上面的代码。我收到以下错误。

prog.cpp: In function 'int dfs(int, const std::set<int>&, int*)':
prog.cpp:8:16: error: no match for 'operator[]' (operand types are 'const std::set<int>' and 'int')          
 for(int y:A[x]){

1 个答案:

答案 0 :(得分:0)

根据c ++文档set - C++ Referenceoperator[]中没有此类std::set<>,因此您的for(int y:A[x])错误。您无法拨打A[x]

我建议使用满足您要求的vector或其他容器,而不是std::set

相关问题