就像我解释的那样
earlier today
您的图形没有顶点索引。如果你想要它有一个,你必须自己添加它。
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
struct MyVertex {
int id;
std::string name;
};
using graph_t = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, MyVertex>;
using Vertex = graph_t::vertex_descriptor;
int main() {
graph_t g;
auto v0 = add_vertex(MyVertex{0, "zero"}, g);
auto v1 = add_vertex(MyVertex{1, "one"}, g);
auto v2 = add_vertex(MyVertex{2, "two"}, g);
auto v3 = add_vertex(MyVertex{3, "three"}, g);
auto v4 = add_vertex(MyVertex{4, "four"}, g);
for (auto [from, to] : { std::pair { v0, v1 }, { v0, v2 }, { v1, v2 }, { v3, v4 }, { v1, v3 }, { v1, v4 } }) {
add_edge(from, to, g);
}
}
现在可以使用ID作为顶点索引:
auto index = get(&MyVertex::id, g);
C++ 11中的PS.
for (auto p : std::vector<std::pair<Vertex, Vertex> > { { v0, v1 }, { v0, v2 }, { v1, v2 }, { v3, v4 }, { v1, v3 }, { v1, v4 } }) {
add_edge(p.first, p.second, g);
}
在C++ 03中写:
Live On Coliru