将 Boost 属性 映射与捆绑类型一起使用
Using Boost Property Maps with Bundled Types
我在获取我正在制作的图表的 属性 地图时遇到问题。我正在使用捆绑属性。我已将其简化为下面的示例。当我尝试获取 IndexMap
的类型时收到错误消息。来自 VC++ 编译器和 gcc 的错误类似于 error forming a reference to void or illegal use of type void
。该错误在 boost 的 adjacency_list.hpp
内部,但是由我对 boost::property_map
的实例化引起的。我一直无法理解正确的类型应该是什么。我在 bundled properties 上阅读了 boost 文档,但发现它们有点无用。有什么想法吗?
编辑:我正在使用 boost 1.67.0。
Edit2:显然将顶点表示更改为 vecS
而不是 listS
可以解决此问题。但是,我更愿意使用 listS
,因为我需要在遍历图形时修改它。
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <string>
#include <memory>
#include <utility>
#include <vector>
struct MyVertex
{
std::string m_name;
};
int main()
{
using graph_t = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, MyVertex>;
using Vertex = boost::graph_traits<graph_t>::vertex_descriptor;
using IndexMap = boost::property_map<graph_t, boost::vertex_index_t>::type;
std::unique_ptr<graph_t> graph;
std::vector<std::pair<int, int>> edges{ {0,1}, {0,2}, {1,2}, {3,4}, {1,3}, {1,4}};
graph = std::make_unique<graph_t>(edges.begin(), edges.end(), 5);
IndexMap index = boost::get(boost::vertex_index, *graph);
return 0;
}
就像我解释的那样你的图表没有顶点索引。如果你想要它有一个,你必须自己添加。
#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);
PS。在 C++11 中写
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
我在获取我正在制作的图表的 属性 地图时遇到问题。我正在使用捆绑属性。我已将其简化为下面的示例。当我尝试获取 IndexMap
的类型时收到错误消息。来自 VC++ 编译器和 gcc 的错误类似于 error forming a reference to void or illegal use of type void
。该错误在 boost 的 adjacency_list.hpp
内部,但是由我对 boost::property_map
的实例化引起的。我一直无法理解正确的类型应该是什么。我在 bundled properties 上阅读了 boost 文档,但发现它们有点无用。有什么想法吗?
编辑:我正在使用 boost 1.67.0。
Edit2:显然将顶点表示更改为 vecS
而不是 listS
可以解决此问题。但是,我更愿意使用 listS
,因为我需要在遍历图形时修改它。
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <string>
#include <memory>
#include <utility>
#include <vector>
struct MyVertex
{
std::string m_name;
};
int main()
{
using graph_t = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, MyVertex>;
using Vertex = boost::graph_traits<graph_t>::vertex_descriptor;
using IndexMap = boost::property_map<graph_t, boost::vertex_index_t>::type;
std::unique_ptr<graph_t> graph;
std::vector<std::pair<int, int>> edges{ {0,1}, {0,2}, {1,2}, {3,4}, {1,3}, {1,4}};
graph = std::make_unique<graph_t>(edges.begin(), edges.end(), 5);
IndexMap index = boost::get(boost::vertex_index, *graph);
return 0;
}
就像我解释的那样
#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);
PS。在 C++11 中写
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