将提升属性映射与捆绑类型一起使用

Using Boost Property Maps with Bundled Types

本文关键字:类型 一起 属性 映射      更新时间:2023-10-16

我在获取正在制作的图形的属性映射时遇到问题。我正在使用捆绑属性。我已将其简化为下面的示例。当我尝试获取IndexMap的类型时收到错误。来自VC ++编译器和gcc的错误类似于error forming a reference to void or illegal use of type void。该错误在 boost 的adjacency_list.hpp内部,但是由我对boost::property_map的实例化引起的。我一直无法理解正确的类型应该是什么。我阅读了捆绑属性的提升文档,但发现它们有点无用。有什么想法吗?

编辑:我正在使用提升 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);

附言。在 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中写道:住在科里鲁