C++ Boost';s属性映射测试是否存在密钥?
在BGL的上下文中,我需要迭代C++ Boost';s属性映射测试是否存在密钥?,c++,boost,boost-graph,boost-property-map,C++,Boost,Boost Graph,Boost Property Map,在BGL的上下文中,我需要迭代In_边和out_边,但我想排除属于反向边的部分,即排除属于反向边的部分属性_映射。下面的代码显示了我希望执行的操作,但是属性映射当然没有find和end方法 更新: 一个可能的解决方案是在构建图形时,保持一个单独的结构,如包含反向边的贴图。如果我控制了图形生成,这将起作用,但我没有,因为我使用函数read\u dimacs\u max\u flow读取dimacs格式的图形文件。因此,我只能依靠BGL的可访问性方法来找出什么是什么 图形定义: typedef ad
In_边
和out_边
,但我想排除属于反向边的部分,即排除属于反向边的部分属性_映射
。下面的代码显示了我希望执行的操作,但是属性映射
当然没有find
和end
方法
更新:
一个可能的解决方案是在构建图形时,保持一个单独的结构,如包含反向边的贴图。如果我控制了图形生成,这将起作用,但我没有,因为我使用函数read\u dimacs\u max\u flow
读取dimacs格式的图形文件。因此,我只能依靠BGL的可访问性方法来找出什么是什么
图形定义:
typedef adjacency_list_traits<vecS, vecS, bidirectionalS> ttraits;
typedef adjacency_list<vecS, vecS, bidirectionalS,
// vertex properties
property<vertex_index_t, int,
property<vertex_color_t, default_color_type> >,
// edge properties
property<edge_capacity_t, int,
property<edge_residual_capacity_t, int,
property<edge_reverse_t, ttraits::edge_descriptor> > >, no_property, vecS> tbgl_adjlist_bidir;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_descriptor tvertex;
typedef graph_traits<tbgl_adjlist_bidir>::edge_descriptor tedge;
typedef property_map<tbgl_adjlist_bidir, edge_capacity_t>::type tedge_capacity_map;
typedef property_map<tbgl_adjlist_bidir, edge_reverse_t>::type treverse_edge_map;
typedef property_map<tbgl_adjlist_bidir, vertex_color_t>::type tvertex_color_map;
typedef property_map<tbgl_adjlist_bidir, vertex_index_t>::type tvertex_index_map;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_iterator tvertex_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::edge_iterator tedge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::out_edge_iterator tout_edge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::in_edge_iterator tin_edge_iterator;
您的
edge\u reverse
属性映射将一个边描述符与图形的每条边相关联。因此,“find”函数没有任何意义,因为所有边在该属性映射中都有相应的条目(请记住,此类属性映射不一定实现为std::map
对象,事实上,它们不属于内部属性)
可以而且应该做的一件事是为每条边设置reverse edge属性的值,使其成为反向边的边描述符或无效的边描述符(对于非反向边)。然后,检查(而不是“查找”)仅仅是检查反向边属性是否为有效边描述符的一种检查
不幸的是,BGL不提供null\u edge()
静态函数(与null\u vertex()
一样)。这可能是开发人员的疏忽(我开发了一些自己的图形结构,并包含了一个null\u edge()
函数,但不是在Boost中)。这意味着很难找到一个好的、可移植的“空边”描述符值来使用。一种选择是使用特定的位模式,如下所示:
ttraits::edge_descriptor my_null_edge;
memset((char*)&my_null_edge, 0xFF, sizeof(ttraits::edge_descriptor));
然后,确保将非反向边的所有反向边属性设置为my\u null\u edge
,然后使用此比较实现循环:
for (tie(ei1, ei1_end) = in_edges(tvertex(current), bgl_adjlist_bidir); ei1 != ei1_end; ++ei1) {
// exclude reverse edges
if (rev_edges[*ei1] != my_null_edge) {
continue;
}
int in = indices[boost::source(*ei1, bgl_adjlist_bidir)];
printf("in edge: %d <- %d \n", current, in);
}
如果需要使用rev_edges
作为BGL属性映射,则可以使用:
typedef boost::associative_property_map< treverse_edge_map > tbgl_reverse_edge_map;
tbgl_reverse_edge_map bgl_rev_edges = boost::make_assoc_property_map(rev_edges);
typedef boost::关联属性映射tbgl\u reverse\u edge\u映射;
tbgl_reverse_edge_map bgl_rev_edges=boost::make_assoc_property_map(rev_edges);
但是,当然,一旦它是BGL样式的属性映射,您就不能再使用“查找”机制来确定是否为给定的边缘设置了属性。感谢您提供了如此详细和信息丰富的答案,提供了很多很好的信息:)但是,我不能直接控制图形的构建,否则我将按照您的建议使用我自己的地图简单地解决它。相反,我使用BGL函数
read_dimacs\u max_flow
来读取dimacs格式的图形文件,因此我只能依靠BGL函数来找出什么是什么,换句话说,这是一个巨大的麻烦:(感谢这篇文章。它给了我很多启发。我正在忍受boost graph library糟糕的文档记录,不得不搜索整个google以找到一种可行的方法来添加正确的参数来运行一些boost max flow算法。现在我将尝试我自己的std::map类型的反向映射,希望它能起作用。
for (tie(ei1, ei1_end) = in_edges(tvertex(current), bgl_adjlist_bidir); ei1 != ei1_end; ++ei1) {
// exclude reverse edges
if (rev_edges[*ei1] != my_null_edge) {
continue;
}
int in = indices[boost::source(*ei1, bgl_adjlist_bidir)];
printf("in edge: %d <- %d \n", current, in);
}
typedef std::map< tedge, tedge > treverse_edge_map;
treverse_edge_map rev_edges;
typedef boost::associative_property_map< treverse_edge_map > tbgl_reverse_edge_map;
tbgl_reverse_edge_map bgl_rev_edges = boost::make_assoc_property_map(rev_edges);