Boost logo

Boost Users :

Subject: Re: [Boost-users] [graph] adjacency_list constructor fails, wrong usage?
From: Anders Wallin (anders.e.e.wallin_at_[hidden])
Date: 2011-11-14 15:54:27


> any suggestions?

If I construct a vector of std::pair<Vertex,Vertex> explicitly I can
get this to compile. But it's hardly an elegant way to copy a graph...

// begin code
#include <iostream>
#include <sstream>

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

int main(int argc, char** argv) {
    typedef boost::adjacency_list < boost::listS, // outedgelist
                                   boost::vecS, // vertexlist
                                   boost::directedS> // directed
    Dgraph;
    typedef boost::graph_traits<Dgraph>::edge_iterator EdgeItr;
    typedef boost::graph_traits<Dgraph>::vertex_descriptor Vertex;

    Dgraph g(5);
    add_edge (0,1,g);
    add_edge (2,3,g);
    add_edge (4,5,g);
    print_graph (g, boost::get(boost::vertex_index, g) );

    EdgeItr i1 = (boost::edges(g)).first;
    EdgeItr i2 = (boost::edges(g)).second;
    std::vector< std::pair<Vertex, Vertex> > edgevector;
    for (EdgeItr e = i1; e!=i2 ; e++ ) {
        std::pair<Vertex, Vertex> p = std::make_pair( source(*e,g),
target(*e,g) );
        edgevector.push_back(p);
    }
    Dgraph g2 = Dgraph( edgevector.begin(), edgevector.end(),
num_vertices(g), num_edges(g));
    print_graph (g2, boost::get(boost::vertex_index, g2));
    return 0;
}
// end code


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net