C ++如何打印图表?

时间:2017-12-10 15:19:24

标签: c++ tuples

所以我把一个包含元组的图形类放在一起,每个元组包含两个取自source.txt的整数,还有一个printGraph函数,但我尝试在我的驱动程序中使用它并且不输出任何内容。我的文本文件在正确的位置,我确定我以正确的方式插入了整数和元组,但我不确定。我认为这与我设置我的打印功能以及我如何实现它有关。如果有人能指出我做错了什么,也许是一个可能的解决方案,我将非常感激。谢谢

void printGraph(Graph t) {

map<int, Vertex*> vertices = t.getVertices();

for (map<int, Vertex*>::iterator it = vertices.begin(); it != vertices.end(); ++it) {
    cout << it->first << ": ";
    vector<Edge *> edges = it->second->getEdges();
    for (vector<Edge *>::iterator jit = edges.begin(); jit != edges.end(); ++jit) {
        int l1 = (*jit)->getV1()->getLabel();
        int l2 = (*jit)->getV2()->getLabel();
        if (l1 != it->first) { cout << l1 << ", "; }
        if (l2 != it->first) { cout << l2 << ", "; }
    }
    cout << endl;
}
}


const int numberStreets = 10 ;
int main() 
{
std::ifstream infile("source.txt");
tuple<int, int> streets[numberStreets];
int a, b;


if (!infile) {
    cout << "File not found" << endl;
    system("pause");
    return 1;
}

int i = 0;
while (infile >> a >> b)
{   
    streets[i] = make_tuple(a, b);
    i++;
}

Graph city(numberStreets);
for (int i = 0; i < numberStreets; i++) 
{
    city.addEdge(get<0>(streets[i]), get<1>(streets[i]));
}
printGraph(city);

system("pause");
return 0;
}

这是我的图表类

class Graph {
vector<Edge*> edges;
map<int, Vertex*> vertices;
public: 
Graph(const int){}
Vertex *  addVertex(int label) {
    Vertex * v = new Vertex(label);
    vertices[label] = v;
    return v;
}
map<int, Vertex*> getVertices() { return vertices; }
vector<Edge*> getEdges() { return edges; }

Edge * addEdge(int from, int to) {

    if (vertices.find(from) != vertices.end() && vertices.find(to) != vertices.end()) {
        Vertex * vfrom = vertices.find(from)->second;
        Vertex * vto = vertices.find(to)->second;
        Edge * e = new Edge(vfrom, vto);
        (*vfrom).addEdge(e);
        edges.push_back(e);
        return e;
    }
    else {

        return 0;
    }
}

Edge * getEdge(int from, int to) {
    if (vertices.find(from) != vertices.end() && vertices.find(to) != vertices.end()) {
        Vertex * v1 = vertices.find(from)->second;
        Vertex* v2 = vertices.find(to)->second;
        Edge * e = (*v1).getEdgeTo(to);
        return e;
    }
    else {
        return 0;
    }
}


void removeEdge(int from, int to) {
    Edge * e = getEdge(from, to);
    if (e != 0) {
        edges.erase(remove(edges.begin(), edges.end(), e), edges.end());
        (*e).getV1()->removeEdge(e);
    }
}

Vertex * getVertexWithLabel(int l) {
    if (vertices.find(l) != vertices.end())
        return vertices.find(l)->second;
    else
        return 0;
}

void removeVertex(int l) {
    Vertex * v = getVertexWithLabel(l);
    if (v != 0) {
        vector<Edge *> edges = getVertexWithLabel(l)->getEdges();

        for (vector<Edge *>::iterator it = edges.begin(); it != edges.end(); ++it) {
            int from = (*it)->getV1()->getLabel();
            int to = (*it)->getV2()->getLabel();
            removeEdge(from, to);
        }
        vertices.erase(l);
    }
    else {

    }
}
};

如果您需要更多信息,请询问并编辑我的问题。谢谢。

Here's what my newest debug was like

1 个答案:

答案 0 :(得分:1)

我注意到的第一个问题是,只调用addEdge,但从不调用addVertex。因此,无论何时拨打addEdgevertices.find(from)vertices.find(to)都会返回vertices.end()(因为顶点不在那里)。

我建议采取以下步骤:

  1. 调试addEdge的调用,看看发生了什么。 (或者,至少检查一下返回值,无论如何都是好的做法。)你会注意到我所描述的问题。

  2. 接听addVertex的来电,再次调试对addVertexaddEdge的调用,以确保此时图形真正构建。 (如果printGraph现在有效,甚至可以进行调试,以确保它按照您的想法发生。)

  3. N.B。:正如我所说,这只是我注意到的第一个问题,所以我不能保证,在添加addVertex个电话后,它会起作用。