如何从boost图库中的边缘迭代器构造adjacency_list?

时间:2018-05-03 13:25:31

标签: c++ boost boost-graph

我有一个与提升图库相关的问题。

boost::adajacency_list有一系列边缘迭代器和顶点数的构造函数。

template <class EdgeIterator>
adjacency_list(EdgeIterator first, EdgeIterator last,
               vertices_size_type n, 
               edges_size_type m = 0, 
               const GraphProperty& p = GraphProperty())

我尝试使用以下代码

来使用此构造函数
#include <boost/graph/adjacency_list.hpp>

using namespace boost;
using Graph = adjacency_list<>;

int main()
{
  using edge_iterator = graph_traits<Graph>::edge_iterator;

  Graph g;
  auto const vd0 = add_vertex(g);
  auto const vd1 = add_vertex(g);
  add_edge(vd0, vd1, g);

  edge_iterator ei, ej;
  tie(ei, ej) = edges(g);

  Graph h(ei, ej, 2);
  return 0;
}

代码无法编译并出现以下错误,

/usr/include/boost/graph/detail/adjacency_list.hpp:2121:29: error: ‘boost::detail::adj_list_edge_iterator<boost::range_detail::integer_iterator<long unsigned int>, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator<boost::detail::stored_edge_property<long unsigned int, boost::no_property>*, std::vector<boost::detail::stored_edge_property<long unsigned int, boost::no_property>, std::allocator<boost::detail::stored_edge_property<long unsigned int, boost::no_property> > > >, long unsigned int, boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>, long int>, boost::adjacency_list<> >::value_type {aka class boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>}’ has no member named ‘first’
           add_edge((*first).first, (*first).second,
                    ~~~~~~~~~^~~~~
/usr/include/boost/graph/detail/adjacency_list.hpp:2121:45: error: ‘boost::detail::adj_list_edge_iterator<boost::range_detail::integer_iterator<long unsigned int>, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator<boost::detail::stored_edge_property<long unsigned int, boost::no_property>*, std::vector<boost::detail::stored_edge_property<long unsigned int, boost::no_property>, std::allocator<boost::detail::stored_edge_property<long unsigned int, boost::no_property> > > >, long unsigned int, boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>, long int>, boost::adjacency_list<> >::value_type {aka class boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>}’ has no member named ‘second’
           add_edge((*first).first, (*first).second,

参考https://www.boost.org/doc/libs/1_37_0/libs/graph/doc/adjacency_list.html

1 个答案:

答案 0 :(得分:1)

如果您只是想要副本,就像您在样本中一样:

Graph h = g;

g属于不同类型:copy_graph¹

您的方法:

std::vector<std::pair<Graph::vertex_descriptor, Graph::vertex_descriptor> > pairlist;
for (auto ed : boost::make_iterator_range(edges(g))) {
    pairlist.emplace_back(source(ed, g), target(ed, g));
}

Graph h(pairlist.begin(), pairlist.end(), 2);

¹另见copy a graph (adjacency_list) to another one

现场演示

显示所有提到的方法:

<强> Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>

using namespace boost;
using Graph = adjacency_list<>;

int main() {
    Graph g;
    add_edge(add_vertex(g), add_vertex(g), g);

    std::vector<std::pair<Graph::vertex_descriptor, Graph::vertex_descriptor> > pairlist;
    for (auto ed : boost::make_iterator_range(edges(g))) {
        pairlist.emplace_back(source(ed, g), target(ed, g));
    }

    Graph h(pairlist.begin(), pairlist.end(), num_vertices(g));
    Graph i = h;
    Graph j;
    boost::copy_graph(h, j);
}