c++ - 深度优先搜索构造函数错误

标签 c++ list depth-first-search

我正在努力使这种深度优先搜索起作用,但我不断收到这个奇怪的错误:

'inputEdges' must be initialized by constructor, not by '{---}'

我不知道如何修复它。

/*
 * Adjancency List
*/
#include <iostream>
 #include <cstdlib>
 #include <bits/stdc++.h>
  using namespace std;

 vector<int> edges[5];
     bool visited[5];



 void dfs(int x)
{
visited[x] = true;
for(int i=0; i < edges[x].size(); i++)
    if(!visited[edges[x][i]])
        dfs(edges[x][i]);


}



/*
* Main function
*/
  int main()
 {


for(int i=0; i < 12; i++)
    visited[i] = false;
vector<pair<int, int> > inputEdges{{0, 1}, {0, 3}, {1, 2}, {1, 3}, {2, 4}, {2, 3}, {4, 5}, {5, 6}, {5, 1}, {3, 9}, {8, 7}, {7, 0}, {9, 1}};
for(int i=0; i < inputEdges.size(); i++)
{
    edges[inputEdges[i].first].push_back(inputEdges[i].second);
    edges[inputEdges[i].second].push_back(inputEdges[i].first);
}

dfs(0);
return 0;
}

最佳答案

通过括号括起来的列表列出 vector 的内容仅在 C++11 或更高版本中有效。您将需要使用符合 C++11 的编译器来编译此代码。

关于c++ - 深度优先搜索构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28889894/

相关文章:

c++ - Qt:自定义LineEditDelegate的Paint函数

list - 多类型列表

c# - 按计算属性搜索对象列表

python - 使用 DFS 或 Greedy BFS 解决解决方案搜索?

algorithm - 使用 HashMap 构建图形?

c++ - xcode ld : 8 duplicate symbols for architecture x86_64

c++ - 使用 C API 在 aspell 中创建自定义词典

c++ - 如何使用 GNU Make 从同一源代码编译程序的第二个版本?

c - 显示打印运行时错误

java - 通过邻接表进行前序遍历