假设我有一个图,每条边都包含一个字符。从一个顶点,我想获得具有特定字符的特定出边。由于边缘容器可以设置为一个集合或一个散列集,我假设有一种方法可以在不遍历顶点的出边的情况下做到这一点。我还假设/希望边缘容器以边缘包含的类型为键。
#include <boost/graph/adjacency_list.hpp>
using namespace boost;
typedef boost::adjacency_list<setS, vecS, directedS, std::string, char> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor Vertex;
typedef boost::graph_traits<MyGraph>::edge_descriptor Edge;
MyGraph g;
//setup
add_vertex(std::string("xxx"), g);
Vertex currentVertex = g.vertex_set()[0];
Vertex endVertex = add_vertex(std::string("yyy"), g);
add_edge(currentVertex, endVertex, 'i', g);
//later...
//Now I want that edge containing the letter 'i'.
//out_edges returns a pair of edge iterators.
std::pair<iterator, iterator> iterators = out_edges(currentVertex, g); // do not want!
Edge iEdge = how_do_I_get_This?(currentVertex, g); // want!
有没有办法做到这一点,或者遍历外边是唯一的选择吗?
更新:
我想这会让我得到容器。
std::set<?> edges = g.out_edge_list(currentVertex);
现在我想不通是什么?模板类型是。
更新2:
这似乎可以编译,但我需要一个 edge_descriptor,而不是一个 edge_property 来传递给目标。
std::set<boost::detail::stored_edge_property<long unsigned int, char> > edges = fGraph.out_edge_list(currentVertex);
更新3:
我猜我不需要边描述符。得到我需要的东西:
std::set<boost::detail::stored_edge_property<long unsigned int, char> > edges = fGraph.out_edge_list(currentVertex);
std::_Rb_tree_const_iterator<boost::detail::stored_edge_property<long unsigned int, char> > edge = edges.find(*i);
Vertex target = edge.get_target();
这一切都可以编译并且似乎可以工作,但它非常丑陋。
最佳答案
您在寻找如何使用边缘描述符吗?
Edge i_edge = add_edge(currentVertex, endVertex, 'i', g).first;
i_edge
是 'i'
边的顶点描述符。
// later...
// Now I want that edge containing the letter 'i'.
char yougotit = g[i_edge];
检查一下:
assert('i' == yougotit);
如果你真的想搜索,并且可以使用 c++1y,你可能会发现这个优雅: Also Live
#include <boost/graph/adjacency_list.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <iostream>
using namespace boost::adaptors;
using namespace boost;
typedef boost::adjacency_list<setS, vecS, directedS, std::string, char> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor Vertex;
typedef boost::graph_traits<MyGraph>::edge_descriptor Edge;
int main() {
MyGraph g;
// setup
add_vertex(std::string("xxx"), g);
Vertex currentVertex = g.vertex_set()[0];
Vertex endVertex = add_vertex(std::string("yyy"), g);
add_edge(currentVertex, endVertex, 'i', g);
for (auto matching : boost::edges(g) | filtered([&g](auto const& e) { return g[e] == 'i'; }))
std::cout << matching << " --> " << g[matching] << "\n";
}
输出:
(0,1) --> i
关于c++ - 在 boost graph lib 中,如何在不遍历该顶点的所有出边的情况下获得顶点的特定出边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26620097/