到目前为止,本章中的所有示例都使用 boost::adjacency_list 来定义图。本节介绍 Boost.Graph 提供的另外两个图形容器:boost::adjacency_matrix 和 boost::compressed_sparse_row_graph。
注意:
Boost 1.56.0 中的 boost/graph/adjacency_matrix.hpp 中缺少包含。要使用 Boost 1.56.0 编译示例 31.15,请在 boost/graph/adjacency_matrix.hpp 之前包含 boost/functional/hash.hpp。
示例 31.15。带有 boost::adjacency_matrix 的图
- #include
- #include
- #include
-
- int main()
- {
- enum { topLeft, topRight, bottomRight, bottomLeft };
-
- std::array
int, int>, 4> edges{ - {
- std::make_pair(topLeft, topRight),
- std::make_pair(topRight, bottomRight),
- std::make_pair(bottomRight, bottomLeft),
- std::make_pair(bottomLeft, topLeft)
- }};
-
- typedef boost::adjacency_matrix
graph; - graph g{edges.begin(), edges.end(), 4};
- }
b