问题
Given an empty boost::graph g, I want to set the number of vertices in this graph and add some edges then. But from the documentation, I cannot find related functions. All examples I found defines the size of vertices in initialization (like Graph g(10) defines a graph with 10 vertices). But I don't know the size when I define the graph. I want to first define a Graph g, and set the size later.
回答1:
The simplest approach is to call the boost::add_vertex( graph ) method for each vertex you want.
Here is a nice place to start Using C++ Boost's Graph Library
Note that you not HAVE to add the vertices one by one. If all you care about are the edges, then add_edge() will add missing vertices for you.
回答2:
You can probably try a little dirty trick like:
add_edge(0,4999,g);
remove_edge(0,4999,g);
It exploits the side-effect of add_edge for adjacency_list, namely, the fact that BGL extends the vector of vertices if necessary.
来源:https://stackoverflow.com/questions/17597655/set-the-number-of-vertices-in-boostgraph