我正在使用boost庫創建一個簡單的圖形,並試圖遍歷頂點和邊。不過,我收到以下錯誤,其中的代碼可以遍歷所有頂點,但有一個編譯問題,當我加入的代碼的最後一段,其負責遍歷邊緣:Boost圖形轉換錯誤
error: conversion from ‘std::pair, boost::detail::out_edge_iter >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list >, boost::detail::adj_list_edge_iterator, boost::detail::out_edge_iter >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list > >’ to non-scalar type ‘std::pair, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator*, std::vector, std::allocator > > >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list<> >, boost::detail::adj_list_edge_iterator, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator*, std::vector, std::allocator > > >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list<> > >’ requested boost::adjacency_list<>::edge_iterator> es = boost::edges(g);
這裏是我的代碼
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
int main()
{
using namespace std;
using namespace boost;
typedef adjacency_list< listS, vecS, directedS > digraph;
// instantiate a digraph object with 8 vertices
digraph g(8);
// add some edges
add_edge(0, 1, g);
add_edge(1, 5, g);
add_edge(5, 6, g);
add_edge(2, 3, g);
add_edge(2, 4, g);
add_edge(3, 5, g);
add_edge(4, 5, g);
add_edge(5, 7, g);
// represent graph in DOT format and send to cout
write_graphviz(cout, g);
std::cout<< "+++++++++++++++++++++++++++++++++++++\n";
std::cout<<"Print Vertices\n";
std::pair<boost::adjacency_list<>::vertex_iterator,
boost::adjacency_list<>::vertex_iterator> vs = boost::vertices(g);
std::copy(vs.first, vs.second,std::ostream_iterator<boost::adjacency_list<>::vertex_descriptor>{
std::cout, "\n"});
std::cout<< "+++++++++++++++++++++++++++++++++++++\n";
std::cout<<"Print Edges\n";
std::pair<boost::adjacency_list<>::edge_iterator,
boost::adjacency_list<>::edge_iterator> es = boost::edges(g);
std::copy(es.first, es.second,std::ostream_iterator<boost::adjacency_list<>::edge_descriptor>{
std::cout, "\n"});
return 0;
}
謝謝,通過使用std :: pair es = boost :: edges(g); –
Mohannad