简体   繁体   English

如何访问图形中顶点边缘的数据?

[英]How to access the data of the edges of the vertex in graph?

I am trying to figure out how I could access the values of the edges of the vertex so that I can print the graph. 我试图弄清楚如何访问顶点边缘的值,以便可以打印图形。

This is my implementation of addEdge: 这是我的addEdge实现:

void clsGraph::add_edge(clsVertex* source, clsVertex* destination, int id, double weght)
{
    clsEdge *a = new clsEdge;
    a->ID = id;
    a->weight = weght;
    a->destination_vertex = destination;
    source->edges.push_back(a);
}

This is my vertex class: 这是我的顶点类:

#ifndef CLSVERTEX_HPP
#define CLSVERTEX_HPP

#include <vector>
#include "clsEdge.hpp"

using namespace std; //namespace Rajkarnikar 
{   
    class clsVertex
    {   
        public:
        int ID; //integer used to uniquely ID a vertex      
        vector<clsEdge*> edges; //a vector of pointers to a vertex’s edges
    }; //
}
#endif

and I have this command to add my vertex: 并且我有以下命令来添加我的顶点:

void clsGraph::add_vertex(clsVertex* vertex)
{
    verticies.push_back(vertex);
}

What I am trying to do is access the vertices and then check if it has any edges or not and output those edges as well. 我想做的是访问顶点,然后检查它是否有边,并输出这些边。

can anyone please help me with this? 有人可以帮我吗?

Thanks for the suggestion :) This was the solution I used... 感谢您的建议:)这是我使用的解决方案...

 cout << ID << " " << graphType << endl;
        for (int i = 0; i < verticies.size(); i++){
            cout << verticies[i]->ID << "->";
            for (int j = 0; j < verticies[i]->edges.size(); j++){
                cout << verticies[i]->edges[j]->ID << endl;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM