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

标签 c++ boost graph

假设我有一个图,每条边都包含一个字符。从一个顶点,我想获得具有特定字符的特定出边。由于边缘容器可以设置为一个集合或一个散列集,我假设有一种方法可以在不遍历顶点的出边的情况下做到这一点。我还假设/希望边缘容器以边缘包含的类型为键。

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

查看 Live On Coliru


如果你真的想搜索,并且可以使用 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/

相关文章:

c++ - 关于 boost::asio::io_context::run 的困惑

java - jgraph布局不起作用

r - 50 个州随时间变化的箱线图

c++ - NTFS:多个 $DATA 属性,同名

c++ - 简化类方法的多重定义

c++ - C++ 中的 Tic-Tac-Toe 帮助,如何制作循环以便 Tic Tac Toe 游戏每次都会重复棋盘

c++ - 多进程c++的锁机制

c++ - Visual Studio 2015 是否允许使用 GCC 编译普通的 C++ 项目?

c++ - 在命名空间中对自己的类使用 boost::program_options::validate

algorithm - 通过在同一点结束的所有顶点遍历加权图