实现图形时未解析的重载函数类型[int]

时间:2018-12-28 11:20:26

标签: c++ graph breadth-first-search

我尝试实现图bfs,但出现编译器错误 错误:

invalid types '<unresolved overloaded function type>[int]' for array subscript|

我的问题:

  1. 我将struct的向量数组作为数组的方法是正确的方法吗?以及如何解决编译器错误?
  2. 如何将数组值初始化为无穷大?
  3. 制作无向图时,我应该推2次吗?

我的代码:

#include <iostream>
#include <deque>
#include <vector>
#define INT_MAX 21422

using namespace std;

int distancee[10]={4,4,4,4,4,4,4,4,4,4}; //want to intialize all to infinity
struct bfss{
    int firstt;
    int secondd;
};

vector<bfss>bfs[10];

void bfsfunc(int start){
    deque<int> q;
    q.push_back(start);
    distancee[start]=0;
    while(!q.empty()){
        int v=q.front();
        q.pop_front();
        for(int i=0;i<bfs[v].size();i++){

            if(distance[bfs[v][i].firstt]>(distance[v]+bfs[v][i].secondd)){ // got error in this line
                distance[bfs[v][i].firstt]=distance[v]+bfs[v][i].secondd;

                if(bfs[v][i].second==0)
                {
                    q.push_front(bfs[v][i].first);
                } else {
                    q.push_back(bfs[v][i].second);
                }
            }
        }
    }
}

int main()
{
    int edges,nodes,x,y,z;
    cin>>edges>>nodes;
    for(int i=0;i<edges;i++){
        cin>>x>>y>>z; //x is array subscript , y is node(x-y is edge) , z is weight

        bfss newbfs;
        newbfs.firstt=y;
        newbfs.secondd=z;
        bfs[x].push_back(newbfs);

        bfss newbfs;
        newbfs.firstt=x;
        newbfs.secondd=z;
        bfs[y].push_back(newbfs);  // when making undirected graph, should i push back 2 times?
    }

    bfsfunc(0);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

如前所述,您在distance,firstt和secondd上有一些错别字。解决这些问题,错误消失。对于int,最接近无限的是最大值。有了这些以及其他一些小的更改(代码中的注释),这就是我想到的:

#include <iostream>
#include <deque>
#include <vector>
#include <array>
#include <limits> // std::numeric_limits

// removed using namespace std;

// max distance: not infinity, but hopefully large enough
constexpr int md = std::numeric_limits<int>::max();

// replaced the C array with a standard C++ array
std::array<int, 10> distancee={md,md,md,md,md,md,md,md,md,md};

struct bfss {   // shouldn't these be unsigned?
    int firstt;
    int secondd;
};

std::vector<std::vector<bfss>> bfs(10); // replaced C array with a standard C++ vector

void bfsfunc(int start) {
    std::deque<int> q;
    q.push_back(start);
    distancee[start]=0;
    while(!q.empty()) {
        int v=q.front();
        q.pop_front();

        // using size_t intstead of int for array subscript
        for(size_t i=0;i<bfs[v].size();i++) {

            if(distancee[bfs[v][i].firstt]>(distancee[v]+bfs[v][i].secondd)) {
                distancee[bfs[v][i].firstt]=distancee[v]+bfs[v][i].secondd;

                if(bfs[v][i].secondd==0) {
                    q.push_front(bfs[v][i].firstt);
                } else {
                    q.push_back(bfs[v][i].secondd);
                }
            }
        }
    }
}

int main()
{
    int edges,nodes,x,y,z;
    std::cin>>edges>>nodes;
    for(int i=0;i<edges;i++) {
        std::cin>>x>>y>>z; //x is array subscript , y is node(x-y is edge) , z is weight

        // using emplace_back to let the vector create the bfss in place
        bfs[x].emplace_back(bfss{y, z});
        bfs[y].emplace_back(bfss{x, z});
    }

    bfsfunc(0);
    return 0;
}

我不知道有关推两次的问题的答案,因为我不知道算法。