yuAriellexi 2018-07-31 06:46 采纳率: 100%
浏览 792
已采纳

这个BFS路径搜索,有关iterator, struct

第一次写bfs, 原理懂了,但是还是写的不明不白

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include  <map>
#include <iterator>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::queue;
using std::vector;
#define MaxVex 10000000


struct edge
{
    int to; //这条管道的终点
//  int Pno;这条管道的编号
    int ifV;//出发点是否是阀门, 1:是; 0:不是
};
vector<edge>::iterator it;//定义迭代器it

vector <edge> Graph[MaxVex];//G[count]是一个vector, vecotr里面包含管段信息,G[count]是出发点
void BFS(vector <edge>, int);



void BFS(vector <edge> G, int v)//graph与搜索源节点v
{
    int flag[MaxVex] = {0};//所有节点均没有被访问过
    queue <int> Q; //与该节点相连接,并且没有被访问过的节点y
    vector<edge>::iterator it;

    Q.push(v);

    while (!Q.empty())
    {
        v = Q.front();
        Q.pop();
        for (it = G[v].begin(); it != G[v].end(); it++)//使用迭代器遍历所有G[v]的edge
        {
            //G[v] vector of edge
            if (flag[(*it).to] == 0 && (*it).ifV == 0)
            {
                cout << "与该点相连的节点编号是 " << (*it).to << endl;
                flag[(*it).to] = 1;
                Q.push((*it).to);
            }
        }
    }   
}

int main()
{
    int n, m;//n个顶点,m组数据
    scanf("%d%d", &n, &m);
    int vex;
    while(m--)
    {
        edge e;
        scanf("%d%d%d", &vex, &e.to, &e.ifV);
        Graph[vex].push_back(e);//G[1]即表格中第一个点的信息
    }
    BFS(Graph, 6);
    return 0;
}

boomAnalysis.cpp:43:18: error: no member named 'begin' in 'edge'
for (it = G[v].begin(); it != G[v].end(); it++)//使用迭...
~~~~ ^
boomAnalysis.cpp:43:38: error: no member named 'end' in 'edge'
for (it = G[v].begin(); it != G[v].end(); it++)//使用迭...
~~~~ ^
boomAnalysis.cpp:73:2: error: no matching function for call to 'BFS'
BFS(Graph, 6);
^~~
boomAnalysis.cpp:31:6: note: candidate function not viable: no known conversion
from 'vector [10000000]' to 'vector' for 1st argument
void BFS(vector G, int v)//graph与搜索源节点v

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-07-31 16:06
    关注

    不涉及算法,从语法的角度说

    edge不是迭代器,而 1ector <edge> 才是

    for (it = G[v].begin(); it != G[v].end(); it++)
    应该是
    for (it = G.begin(); it != G.end(); it++)

    void BFS(vector <edge> G, int v)
    需要的G是vector而不是vector 数组
    BFS(Graph, 6);
    传入的是整个vector构成的数组,而不是单个vector<edge>

    综合以上两点,我估计你对参数G的认识有问题,你函数内一直把它当作vector<edge>构成的数组(从edge的角度看,就是二维数组了),但是调用的时候又当成单个vector

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog