【发布时间】:2020-01-11 11:37:46
【问题描述】:
主要问题:
我能够创建一个图实现,并将信息结构分配给顶点和边:
struct vertex_info {std::string name;};
struct edge_info {std::string name;};
typedef boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS,
vertex_info,
edge_info> UndirectedGraph;
对于UndirectedGraph、g 的实例,我可以轻松地遍历顶点并访问它们的信息:
for(size_t i=0; i<boost::num_vertices(g); i++){
std::cout << g[i].name << std::endl;
}
但我无法弄清楚如何对边缘做同样的事情。我遇到了一些迭代器来遍历所有边缘,但是我无法将这些边缘作为某种对象或具有属性的东西来访问。如何获取g的边缘信息?
一个最小的工作演示:
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include "boost/graph/graph_traits.hpp"
#include "boost/graph/adjacency_list.hpp"
int main(int argc, char *argv[])
{
//Add vertex information struct
struct vertex_info {
std::string name;
};
//Add edge information struct
struct edge_info {
std::string name;
};
//Typedef my graph implimentation
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, vertex_info, edge_info> UndirectedGraph;
//Our set of edges, and count N: (0-7) and 8
enum {C, D, G, I, S, J, L, H, N};
const char *name = "CDGISJLH";
//Create a vector of edges
typedef std::pair<int, int> Edge;
std::vector<Edge> edgeVec;
edgeVec.push_back(Edge(C,D));
edgeVec.push_back(Edge(D,G));
edgeVec.push_back(Edge(I,G));
edgeVec.push_back(Edge(G,L));
edgeVec.push_back(Edge(H,G));
edgeVec.push_back(Edge(I,S));
edgeVec.push_back(Edge(S,J));
edgeVec.push_back(Edge(L,J));
edgeVec.push_back(Edge(H,J));
//Now we can initialize our graph using iterators from our above vector
UndirectedGraph g(edgeVec.begin(), edgeVec.end(), N);
std::cout << num_edges(g) << "\n"; //Outputs: 9
//loop over vertices, access "name" property
for(size_t i=0; i<boost::num_vertices(g); i++){
//And add information to the edges
g[i].name = "foo";
}
//We can access the first vertice and print the property
std::cout << g[0].name << std::endl; //Outputs: foo
//Edge iterator for or graph
typedef boost::graph_traits<UndirectedGraph>::edge_iterator edge_iterator;
//Iterate through all the edges
std::pair<edge_iterator, edge_iterator> ei = boost::edges(g);
for(edge_iterator edge_iter = ei.first; edge_iter != ei.second; ++edge_iter) {
//How can I access the edge property???
}
}
【问题讨论】:
-
试过
edge_iter->name?文档说“Dereferencing an edge iterator gives an edge object.”-参考boost.org/doc/libs/1_35_0/libs/graph/doc/quick_tour.html -
@jweyrich 你会这么认为,对,但不幸的是:
‘class boost::detail::edge_desc_impl<boost::undirected_tag, long unsigned int>’ has no member named ‘name’; did you mean ‘Base’? -
根据文档,取消引用
edge_iter指针应该会给您一个边缘对象 - 在您的情况下,它的类型是edge_info并具有名为name的命名成员。也许我以某种方式误读了文档。 -
@jweyrich,是的,我也这样解释文档,但似乎不起作用。而且我找不到简单的解决方法。这就是发生的事情:coliru.stacked-crooked.com/a/5d0766e1770c179b。现在,我将只使用像
map<pair>这样的单独容器来存储边缘信息,直到我弄清楚为止。