Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用Boost Graph Library在0和1之间添加一个顶点,以便存在一条从0到0.5(比如新顶点)和0.5到1的边?_C++_C++11_Boost Graph - Fatal编程技术网

C++ 如何使用Boost Graph Library在0和1之间添加一个顶点,以便存在一条从0到0.5(比如新顶点)和0.5到1的边?

C++ 如何使用Boost Graph Library在0和1之间添加一个顶点,以便存在一条从0到0.5(比如新顶点)和0.5到1的边?,c++,c++11,boost-graph,C++,C++11,Boost Graph,我有一个助推图: Graph g(2); 我访问的索引如下: g[0]; g[1]; 我画了一条边 add_edge(0, 1, g); 问题: 我想在0和1之间添加一个顶点,这样就存在一条从0到0.5(比如新顶点)和0.5到1的边 我的方法应该是什么?给定原始边的边描述符: template <typename Edge, typename Graph> auto split_edge(Edge edge, Graph& g) { auto v = sourc

我有一个助推图:

Graph g(2);
我访问的索引如下:

g[0]; g[1];
我画了一条边

add_edge(0, 1, g);
问题:

我想在0和1之间添加一个顶点,这样就存在一条从0到0.5(比如新顶点)和0.5到1的边


我的方法应该是什么?

给定原始边的边描述符:

template <typename Edge, typename Graph>
auto split_edge(Edge edge, Graph& g) {
    auto v = source(edge, g);
    auto u = target(edge, g);

    auto n = add_vertex(g);

    add_edge(v, n, g);
    add_edge(n, u, g);
    return n;
}

请花点时间看看和。
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>

using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>;

template <typename Edge, typename Graph>
auto split_edge(Edge edge, Graph& g) -> typename Graph::vertex_descriptor {
    auto v = source(edge, g);
    auto u = target(edge, g);

    auto n = add_vertex(g);

    add_edge(v, n, g);
    add_edge(n, u, g);
    return n;
}

int main() {
    Graph g(2);
    auto original = add_edge(0, 1, g).first;
    print_graph(g);

    auto v_half = split_edge(original, g);

    print_graph(g);
}
0 --> 1 
1 --> 
0 --> 1 2 
1 --> 
2 --> 1