在boost图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?

本文关键字:边缘 顶点 情况下 boost 迭代 lib      更新时间:2023-10-16

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

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

CCD_ 1是CCD_ 2边的顶点描述符。

// 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