C++ 在boost graph库中,如何在不迭代顶点的所有外边缘的情况下获得顶点的特定外边缘?

C++ 在boost graph库中,如何在不迭代顶点的所有外边缘的情况下获得顶点的特定外边缘?,c++,boost,graph,C++,Boost,Graph,假设我有一个图,每个边都包含一个字符。从一个顶点,我想得到一个具有特定字符的特定外边缘。由于边缘容器可以设置为集合或散列集合,因此我假设有一种方法可以做到这一点,而无需迭代顶点的外边缘。我还假设/希望边缘容器是在边缘包含的类型上设置关键帧的 #include <boost/graph/adjacency_list.hpp> using namespace boost; typedef boost::adjacency_list<setS, vecS, directedS, s

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

#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!
#包括
使用名称空间boost;
typedef boost::邻接列表MyGraph;
typedef boost::graph_traits::vertex_描述符顶点;
typedef boost::graph_traits::edge_描述符edge;
myg图;
//设置
添加顶点(std::string(“xxx”),g);
顶点currentVertex=g.顶点集合()[0];
顶点端点=添加顶点(标准::字符串(“yyy”),g);
添加_边(currentVertex,endVertex,'i',g);
//后来。。。
//现在我想要包含字母“I”的边。
//out_edge返回一对边迭代器。
std::pair iterators=out_边(currentVertex,g);//不要!
Edge iEdge=如何得到这个?(currentVertex,g);//希望
有没有一种方法可以做到这一点,或者说,通过输出边进行迭代是唯一的选择

更新:

我想这会让我拿到集装箱

std::set<?> edges = g.out_edge_list(currentVertex);
std::set edges=g.out\u edge\u列表(currentVertex);
现在我想不出是什么原因?模板类型为

更新2:

这似乎可以编译,但我需要一个edge_描述符,而不是一个edge_属性来传递给目标

 std::set<boost::detail::stored_edge_property<long unsigned int, char> > edges = fGraph.out_edge_list(currentVertex);
std::set edges=fGraph.out\u edge\u列表(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();
std::set edges=fGraph.out\u edge\u列表(currentVertex);
std::_Rb_tree_const_iterator edge=edges.find(*i);
顶点目标=边。获取_目标();

所有这些都可以编译并且似乎可以工作,但是它非常难看。

您正在寻找如何使用边缘描述符吗

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);
查看它

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

如果您真的想搜索,并且可以使用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";
}

事实上,我想要的是:给定一个顶点,找出从该顶点发出的边,该顶点标记为“I”。所以边e=得到特定的顶点(顶点,g,'i');。我的更新3设法做到了这一点,但方式有些丑陋。我已经将边配置为一组,所以希望这意味着对于每个顶点,只能有一条“I”边。