我设法使用以下捆绑的顶点和边属性来创建图形:
struct vertexproperties
{
// index
int id;
// shortest distance to all other nodes
std::vector<float> dist_to;
// shortest distance from all other nodes
std::vector<float> dist_from;
// constructors (note: empty version is necessary for graph)
vertexproperties (int id) : id(id) {}
vertexproperties () : id() {}
};
和
struct edgeproperties
{
// weight
float weight;
// resources
std::vector<float> res;
// constructors (note: empty version is necessary for graph)
edgeproperties (float weight, int n_res) : weight(weight), res(n_res, 0) {}
edgeproperties () : weight(0.0), res() {}
};
并且我设法使用
令人满意地创建了图形
typedef adjacency_list<listS, vecS, directedS, vertexproperties, edgeproperties> graph_t;
但是,我有两个问题:一个是关于求解最短路径时如何使用矢量值作为属性映射的问题,例如关于Dijkstra的问题。另一个是关于在vertexproperties
和edgeproperties
中使用空构造函数的明显必要性:
受boost bundled properties documentation启发,我尝试使用
dijkstra_shortest_paths(g, s, weight_map(get(&edgeproperties::res, g)));
要解决,我得到了test.cpp:180:41: error: invalid use of non-static data member ‘edgeproperties::res’
形式的错误,这并不奇怪,因为我刚刚传递了一个向量,而不仅仅是传递一个标量。
由于我不知道要执行此操作,因此我尝试使用get(&edgeproperties::res[0], g)
和get(&edgeproperties::res, g)[0]
对此进行补救,但两者均无效。
第一个问题是如何执行此操作?
看起来有必要提供构造器vertexproperties () : id() {}
和edgeproperties () : weight(0.0), res() {}
,它们和非空的构造器实际上并没有什么关系。
我不愿意添加它们,因为它们永远都不应使用:对于一个顶点,id只能为0,而对于其他顶点,ID则必须不同。
因此,我想知道是否真的有必要提供这些空的构造函数和/或我是否在其中做错了事。