将 public 属性设置为私有

Set a public attribute as private

class vertex{

    // ...

};

我正在尝试将此 public 属性设置为私有。

vector<edge> edges;

但是这里

void addedge(int from, int to, int length=-1) {
    vertices[from].edges.push_back(edge(to, length));
}

这里

edge &e = vertices[u].edges[i];

我无法理解如何使用其他情况下的方法访问边缘。

完整代码:

#define MAX_VER 1000
#define INFINITE 9999
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
class edge{
    private:
        int to;
        int length;
    public:
        edge(int to, int length) : to(to), length(length) {}
        int getTo(){return to;};
        void setTo(int t){to=t;};
        int getL(){return length;};
        void setL(int l){length=l;};
};
class vertex{
    private:
        //vector<edge> edges;
        int dis;
        int prev;
    public:
        vector<edge> edges;

        vector<edge> get_edges(){return edges;}

        int getDis(){return dis;};
        void setDis(int d){dis=d;};
        int getPrev(){return prev;};
        void setPrev(int p){prev=p;};
};
class graph{
    private:
        vertex vertices[MAX_VER];
    public:
        void reset() {
            for (int i=0; i < MAX_VER; i++) {
                vertices[i].get_edges().clear();
                vertices[i].setDis(INFINITE);
                vertices[i].setPrev(-1);
            }
        }

        void addedge(int from, int to, int length=-1) {
            vertices[from].edges.push_back(edge(to, length));
        }
        typedef pair<int, int> pp;
        void dijkstra(int source) {
            priority_queue<pp, vector<pp>, greater<pp> > q;
            vertices[source].setDis(0);
            q.push(make_pair(0, source));
            while (!q.empty()) {
                int u = q.top().second;
                int dis = q.top().first;
                q.pop();
                if (dis > vertices[u].getDis())
                    continue;
                for (int i = 0; i < vertices[u].get_edges().size(); i++) {
                    edge &e = vertices[u].edges[i];
                    if (dis + e.getL() < vertices[e.getTo()].getDis()) {
                        vertices[e.getTo()].setDis(dis + e.getL());
                        vertices[e.getTo()].setPrev(u);
                        q.push(make_pair(vertices[e.getTo()].getDis(), e.getTo()));
                    }
                }
            }
            cout << "Distance from vertex 2 to 4 is: " << vertices[4].getDis() << endl;
        }
};
int main() {
    graph g;
    g.reset();
    g.addedge(0, 1, 5);
    g.addedge(0, 2, 9);
    g.addedge(0, 3, 4);
    g.addedge(0, 4, 6);
    g.addedge(1, 2, 2);
    g.addedge(1, 3, 5);
    g.addedge(1, 4, 7);
    g.addedge(2, 3, 1);
    g.addedge(2, 4, 8);
    g.addedge(3, 4, 3);
    g.dijkstra(2);
    return 0;
}

edges 应该 是私有成员,可以通过 public get 函数访问。您已经定义了类似这样的内容 (get_edges()),但它没有正确定义:

class vertex
{
    //...

public:
    vector<edge> edges;

    vector<edge> get_edges(){return edges;}

    //...
};

您 return 通过值成员,这意味着每次调用此函数时,都会创建 edges 新副本 并且该副本已 returned!

您应该 return 它作为参考并提供 constnon-const 对象的版本:

class vertex
{
    //...

public:
    vector<edge> edges;

    vector<edge>& get_edges() { return edges; }
    const vector<edge>& get_edges() const { return edges; }

    //...
};

此外,edges 仅供 class graph 使用。所以可能这就足以宣布这两个 classes 之间的友谊?

vertexclass改成这样:

class vertex
{
    friend class graph; //declare friend

private:
    int dis;
    int prev;
    vector<edge> edges; //make 'edges' private

public:
   vector<edge>& get_edges() { return edges; }
   const vector<edge>& get_edges() const { return edges; }
};

从现在开始,graph class 将可以访问 vertex class 的所有私有和受保护成员,因为它已被声明为它的好友。在简单的情况下,这是最快且侵入性较小的解决方案(但仅就需要编写的额外代码而言)。