c++ - 为什么我的 for 循环给出错误 : X does not name a type

标签 c++ for-loop graph auto

谁能告诉我,当我使用 for 循环获取存储在级别“it”中的节点数时,为什么它不起作用?

请告诉我其他方法可以访问具有基于范围的 for 循环的 vector 。

// 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 , 那么 vector 应该是 vector adj[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;
    }
    

    我已经看过hackerearth问题了。但只是试图解决你的问题。

    但是,如果您无论如何都想要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;
    }
    
    

    希望它可能会有所帮助。

    关于c++ - 为什么我的 for 循环给出错误 : X does not name a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61982046/

    相关文章:

    c++ - 来自编程珍珠的字符串函数

    c++ - 将 OpenMPI 或 MPICH 与 Boost MPI 一起使用 Win 和 Linux 机器

    algorithm - 给定顶点的度数,检查是否存在无向图

    c++ - 随机访问(或以其他方式快速访问)boost 图形库中的边

    c - 从文件(C 语言)中读取整数,其中包括有向图的数据

    c++ - 定义一个模板类但是得到 'is not a class template'

    c++ - 重载纯虚拟运算符

    php - 在for循环中生成查询结果

    C - strcspn() 函数跳过数组中较长的字符串

    Excel VBA : "For" and "If" statement on a single line?