繁体   English   中英

在boost graph lib中,如何在不遍历该顶点的所有边缘的情况下获得该顶点的特定边缘?

[英]in boost graph lib, how do I get a specific out-edge of a vertex without iterating over all the out-edges of that vertex?

假设我有一个图,每个图的边都包含一个字符。 从一个顶点,我想获得具有特定字符的特定边缘。 由于可以将边缘容器设置为一个集或一个哈希集,因此我认为有一种方法可以做到这一点而无需遍历顶点的边缘。 我还假设/希望边缘容器键入边缘包含的类型。

#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);

现在我不知道是什么? 模板类型为。

UPDATE2:

这似乎可以编译,但是我需要一个edge_descriptor,而不是edge_property才能传递给目标。

 std::set<boost::detail::stored_edge_property<long unsigned int, char> > edges = fGraph.out_edge_list(currentVertex);

UPDATE3:

猜猜我不需要边缘描述符。 得到了我需要的东西:

 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);

在Coliru上实时观看


如果你真的想进行搜索,并且可以使用C ++ 1Y你可能会发现这个优雅: 还住

#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

暂无
暂无

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

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