将提升属性映射与捆绑类型一起使用
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
的实例化引起的。我一直无法理解正确的类型应该是什么。我阅读了捆绑属性的提升文档,但发现它们有点无用。有什么想法吗?
编辑:我正在使用提升 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中写道:住在科里鲁
相关文章:
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 将 lower_bound/upper_bound 与 2 种不同的类型一起使用
- 不允许将SDL_Cursor与unique_ptr:error不完整类型一起使用
- 将提升属性映射与捆绑类型一起使用
- 将运算符<<与隐式转换的非基本数据类型一起使用时出错
- 将分配的内存与基本数据类型一起使用时,是否需要新放置? std::complex?
- 如何将模运算符与其他数据类型一起使用
- 使用#Define和Typedef与数据类型A一起使用
- 如何编写操作员==与隐式铸造/构造类型一起使用
- 测试 std::p ointer_traits 是否可以与我的类型一起使用
- 是否将invoke_result与void参数类型一起使用
- 将CloudViewer与自定义Pointt类型一起使用
- 将C++模板函数与生成器类型一起使用
- 如何将 std::enable_if 与自推返回类型一起使用
- QMetaType与模板化类型一起工作吗
- 如何将 cin 与未知输入类型一起使用
- 将make_shared与不完整的类型一起使用
- 如何将unordered_set与自定义类型一起使用
- 将"const"与标量类型一起使用的好处?(例如 "const double"或"const int")
- 偏移量可以与从decltype获得的结构类型一起使用吗?