C++ 绑定到boost图形库中std::vector的外部属性映射

C++ 绑定到boost图形库中std::vector的外部属性映射,c++,boost,graph,boost-graph,boost-property-map,C++,Boost,Graph,Boost Graph,Boost Property Map,我目前正在尝试定义boost图的外部属性。我使用一些捆绑属性作为内部属性: struct VertexProperties { int demand; }; struct EdgeProperties { uint capacity; int cost; }; typedef adjacency_list <vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph; struct ver

我目前正在尝试定义boost图的外部属性。我使用一些捆绑属性作为内部属性:

struct VertexProperties
{
  int demand;
};

struct EdgeProperties
{ 
  uint capacity;
  int cost;
};

typedef adjacency_list <vecS, vecS, bidirectionalS, VertexProperties, EdgeProperties> Graph;
struct vertexproperty
{
国际需求;
};
结构边属性
{ 
单位容量;
国际成本;
};
typedef邻接列表图;
但是,在算法期间,我需要一些外部属性,即我希望能够将图形的边/顶点映射到存储在std::vector中的元素,这样我就可以通过操作符[](边e)访问它们。我站在boost文档前面,一点线索也没有。看起来我需要一个属性映射,但我不知道如何将它们与向量一起使用。到目前为止,我发现的唯一示例涉及从顶点到向量的映射,但由于顶点是无符号整数,这很简单


到目前为止,boost真的让我很沮丧,我想这会节省我很多时间来实现和测试一个graph类,我真的不懂这种疯狂的模板元编程的东西…

你可以创建外部属性映射,不管你的图中有什么内部和/或捆绑属性。在边上创建属性映射有些困难,因为您需要一个
edge\u索引
map,而
邻接列表
默认情况下没有这些索引<代码>压缩的\u稀疏的\u行\u图可以,但它的结构在构造后大部分是只读的。您可以在边上使用
关联属性映射
,或者创建一个边索引映射作为内部属性(如果您不经常更改图形),填充它,然后使用它来构建外部属性映射(例如,使用
共享数组属性映射
),下面是我如何附加顶点属性(在代码段中称为degree)的结果:

//图形必须具有_index_属性(基于向量的图形隐式获取)
typedef typename属性_映射::type IndexMap;
//_degree_属性的存储类型
typedef std::向量除梯度因子;
//_度u属性映射的类型
typedef迭代器属性映射DegreeMap;
//有问题的图表
图g(5);
//实际存储
DegreeVector度存储(顶点数(g));
//这是构建_度u属性映射所必需的
IndexMap index\u map=get(顶点索引,g);
//创建_度u属性映射
DegreeMap degree\u map=生成迭代器属性映射(degree\u storage.begin(),index\u map);
//现在可以使用度图了
度映射[某些顶点id]=10;

Boost.Graph处于临界不可用状态;它试图过于通用,文档也很差。我建议你自己写一个类。好吧,我已经用boost图写了很多代码,我不想重写所有这些…如果删除图的顶点,这能正常工作吗?谢谢
// Graph has to have _index_ property (vector-based graphs get it implicitly)
typedef typename property_map<Graph, vertex_index_t>::type IndexMap;
// Storage type for the _degree_ property
typedef std::vector<uint> DegreeVector;
// Type of the _degree_ property map
typedef iterator_property_map<typename DegreeVector::iterator, IndexMap> DegreeMap;

// Graph in question
Graph g(5);
// Actual storage
DegreeVector degree_storage(num_vertices(g));
// This is needed to construct _degree_ property map
IndexMap index_map = get(vertex_index, g);
// Create _degree_ property map
DegreeMap degree_map = make_iterator_property_map(degree_storage.begin(), index_map);

// Now degree_map is ready to be used
degree_map[some_vertex_id] = 10;