为什么我的 for 循环给出错误:X 没有命名类型
Why is my for loop giving error : X does not name a type
谁能告诉我,当我使用 for 循环来获取存储在级别"it"中的节点数量时,为什么它不起作用?
请告诉我其他方法可以访问具有基于范围的 for 循环的矢量。
// A simple representation of graph using STL
#include<iostream>
#include<vector>
using namespace std;
// A utility function to add an edge in an
// undirected graph.
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void printNodes(vector<int> adj[], int n)
{ int count=0;
for (auto x : adj[n]){
count++;
}
cout<<count;
}
// Driver code
int main()
{
int V,x,y;
cin>>V;
vector<int> adj[V+1];
for(int i=0;i<V-1;i++){
cin>>x>>y;
addEdge(adj, x, y);
}
int it;
cin>>it;
printNodes(adj, it);
return 0;
}
首先,您的方法中缺少一些东西:
- 您在图形中添加边缘并且没有输入,我假设 V 仅用于顶点。
- 每当您想检查特定级别的节点数量时,您都应该始终提及起点。因为不同的起点可能会解释图中的不同输出,而图形不像树那样扎根。
- 如果顶点数为 V,则向量应为 vectoradj[V+1],如果您希望顶点为 1 索引。
所以这是最终代码:
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int nodes_at_level[10];
// Taken from hackerearth.....
int level[10]; //To determine the level of each node
bool vis[10]; //Mark the node if visited
void bfs(int s,vector<int> adj[]) {
queue <int> q;
q.push(s);
level[ s ] = 0 ; //Setting the level of the source node as 0
nodes_at_level[level[s]]++;
vis[ s ] = true;
while(!q.empty())
{
int p = q.front();
q.pop();
for(int i = 0;i < adj[ p ].size() ; i++)
{
if(vis[ adj[ p ][ i ] ] == false)
{
//Setting the level of each node with an increment in the level of parent node
level[ adj[ p ][ i ] ] = level[ p ]+1;
nodes_at_level[level[ adj[ p ][ i ] ]]++;
q.push(adj[ p ][ i ]);
vis[ adj[ p ][ i ] ] = true;
}
}
}
}
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
//to print the number of nodes in the level 'it'
void printNodes(vector<int> adj[], int n)
{
int count = 0;
count = nodes_at_level[n];
cout << count;
}
// Driver code
int main()
{
int V, x, y ,E;
cin >> E;
cin >> V;
vector<int> adj[V+1];
for (int i = 0; i < E; i++) {
cin >> x >> y;
addEdge(adj, x, y);
}
bfs(1,adj); //assuming the start of the graph to be 1
int it;
cin >> it;
printNodes(adj, it);
return 0;
}
我见过黑客地球的问题。但只是试图解决你的问题。
但是,如果您无论如何都想要 for 循环,请尝试这个...我只是根据它们的级别存储顶点..
vector<int> nodes_at_level[10];
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Taken from hackerearth.....
int level[10]; //To determine the level of each node
bool vis[10]; //Mark the node if visited
void bfs(int s,vector<int> adj[]) {
queue <int> q;
q.push(s);
level[ s ] = 0 ; //Setting the level of the source node as 0
nodes_at_level[level[s]].push_back(s);
vis[ s ] = true;
while(!q.empty())
{
int p = q.front();
q.pop();
for(int i = 0;i < adj[ p ].size() ; i++)
{
if(vis[ adj[ p ][ i ] ] == false)
{
//Setting the level of each node with an increment in the level of parent node
level[ adj[ p ][ i ] ] = level[ p ]+1;
nodes_at_level[level[ adj[ p ][ i ] ]].push_back(adj[p][i]);
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
q.push(adj[ p ][ i ]);
vis[ adj[ p ][ i ] ] = true;
}
}
}
}
//to print the number of nodes in the level 'it'
void printNodes(vector<int> adj[], int n)
{
int count=0;
for (auto x : nodes_at_level[n]){
count++;
}
cout<<count;
}
希望它可能会有所帮助。
您的问题在于输入 for 循环的停止条件
for (int i = 0; i < V; i++) {
cin >> x >> y;
addEdge(adj, x, y);
}
你跑到 V -1(在网站上的例子 V=20( 但是你没有索引 20因为性感开始为 0 因此,当您尝试访问单元格 20 时,您有索引 0-19,您会得到 sigmantion fualt。
您必须选择如何解决此问题 1) 做循环
for (int i = 0; i < V-1; i++)
或设置矢量vector<int> adj[V+1];
下次尝试使用调试器时,它将帮助您立即看到问题
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误