简体   繁体   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?

Let's say I have a graph, with edges each containing a char. 假设我有一个图,每个图的边都包含一个字符。 From a vertex, I want to get a specific out-edge with a specific char. 从一个顶点,我想获得具有特定字符的特定边缘。 Since the edge container can be set to a set or a hash-set, I assume there is a way to do this without iterating through the vertex's out-edges. 由于可以将边缘容器设置为一个集或一个哈希集,因此我认为有一种方法可以做到这一点而无需遍历顶点的边缘。 I'm also assuming/hoping the edge container is keyed on the type the edge contains. 我还假设/希望边缘容器键入边缘包含的类型。

#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!

Is there a way to do this, or is iterating through the out-edges the only option? 有没有办法做到这一点,还是在边缘进行迭代是唯一的选择?

update: 更新:

I think this will get me the container. 我认为这将为我提供容器。

std::set<?> edges = g.out_edge_list(currentVertex);

Now I cannot figure out what the ? 现在我不知道是什么? template type is. 模板类型为。

update2: UPDATE2:

This seems to compile, but I need an edge_descriptor, not an edge_property to pass to target. 这似乎可以编译,但是我需要一个edge_descriptor,而不是edge_property才能传递给目标。

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

update3: UPDATE3:

Guess I don't need an edge descriptor. 猜猜我不需要边缘描述符。 Got what I needed like this: 得到了我需要的东西:

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

This all compiles and seems to work, but it is massively ugly. 所有这些都可以编译并且似乎可以工作,但是它非常难看。

Are you looking for how to use edge descriptors? 您是否正在寻找如何使用边缘描述符?

Edge i_edge = add_edge(currentVertex, endVertex, 'i', g).first;

i_edge is the vertex-descriptor for the 'i' edge. i_edge'i'边缘的顶点描述符。

// later...
// Now I want that edge containing the letter 'i'.
char yougotit = g[i_edge];

Check it: 核实:

assert('i' == yougotit);

See it Live On Coliru 在Coliru上实时观看


If you really want to search, and can use c++1y you might find this elegant: Also Live 如果你真的想进行搜索,并且可以使用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";
}

Output: 输出:

(0,1) --> i

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

相关问题 Boost:如何去除顶点的所有出边 - Boost: how to remove all the out-edges for a vertex 如何获得有向图上给定顶点的输入边? - How can I get input edges of a given vertex on a directed graph? Boost有向图:比较顶点的边 - Boost directed graph: comparing edges of a vertex boost::graph:如何删除先前删除的顶点的入边? - boost::graph: How to remove in-edges of a previously removed vertex? 如何将boost :: graph算法与listS,setS一起用作顶点/边缘容器? - How to use boost::graph algorithms with listS, setS as vertex/edge containers? BGL 无向图出边迭代器源/目标 - BGL Undirected Graph Out-Edge Iterator Source/Target Boost 图——将顶点/边存储为 std::list 并随后随机访问关联的顶点/边属性 map - Boost graph -- storing vertices / edges as std::list and subsequent random access of associated vertex / edge property map 没有edge_predicate的BOOST filter_graph中的out_edges()实现 - out_edges() implementation in BOOST filtered_graph without edge_predicate 为什么我不能使用我的无序 map 从 boost 图形库中打印出这个顶点的名称 - Why can't I use my unordered map to print out a name for this vertex from boost graph library Boost Graph Library:如何保持对顶点的永久引用? (即不受顶点重新编号) - Boost Graph Library: How do I keep a permanant reference to a vertex? (i.e. not subject to vertex re-numbering)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM