使用BOOST复制边及其顶点和特性
copying edges with their vertices and properties using BOOST
我想从dataG.front()中复制带有顶点和属性的边,并将其添加到testg中,我尝试了在"访问捆绑属性"部分中找到的内容http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/bundles.html但它对我不起作用。PS:dataG是一个图的向量。
typedef std::pair<edge_iter, edge_iter> edge_pair;
Graph testg;
if (!dataG.empty())
{
auto const& gr = dataG.front();
for (edge_pair ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number
{
auto ep = edges(gr).first; // ep edge number
vertex_t from = source(*ep.first, gr);
vertex_t to = target(*ep.first, gr);
boost::add_vertex(gr[from], testg);
boost::add_vertex(gr[to], testg);
boost::add_edge(from, to, gr[*ep.first], testg);
}
}
边属性有效,但在源和目标中存在问题。(vertex_t和add_vertex部分),如何将顶点属性直接添加到添加的属性中,因为这里有重复。
附言:这里是完整的代码http://pastebin.com/2iztGAa6
正如您所注意到的,顶点可能是重复的,如果您将多个源图"合并"到一个图中,情况尤其如此。
如果你不介意重写顶点属性(并保留上次分配的值,以防值一直不相同),你可以使用属性映射:
boost::property_map<Graph, boost::vertex_bundle_t>::type vpmap = boost::get(boost::vertex_bundle, testg);
//so:
vpmap[from] = gr[from];
vpmap[to] = gr[to];
再说一遍,还有类似的等价访问:
testg[from] = gr[from];
testg[to] = gr[to];
您甚至可以寻址单个捆绑成员:
boost::property_map<Graph, int VertexProperties::*>::type idmap = boost::get(&VertexProperties::id, testg);
boost::property_map<Graph, int VertexProperties::*>::type labelmap = boost::get(&VertexProperties::label, testg);
idmap[from] = gr[from].id;
labelmap[from] = gr[from].label;
基于此文档页面的所有样本
相关文章:
- Boost Read_graphml 无法正确读取 xml,它给出了所有顶点,但它们是空的
- 使用Vertex_descriptor boost图在顶点上打印或迭代
- 如何在不重新索引顶点的情况下调用"boost::remove_vertex"?
- Boost:列出GraphMl中指定的顶点和边缘属性
- 在Boost Graph库中选择给定顶点的随机进出邻居的有效方法
- 如何使用boost::graph dijkstra的算法,如果顶点属性是指针?
- 使用Boost :: Graph随机访问顶点
- C Boost ::如何从顶点描述符创建地图
- 在BOOST图中找到给定2个顶点的多条边
- Boost:如何移除顶点的所有外边缘
- 通过允许用户选择顶点数量,使用Boost库生成图形
- Boost图库undirected_Graph:如何指定顶点类型(例如int)
- 从 boost::adjacency_list 获取边属性(包括相关顶点)
- 使用BOOST复制具有相邻顶点的边及其属性
- 使用BOOST复制边及其顶点和特性
- 从Boost Graph中删除多个顶点
- Boost有向图:比较顶点的边
- 为什么Boost图库在删除顶点时会使所有迭代器无效
- Boost:: Dijkstra 最短路径,如何从路径迭代器获取顶点索引
- 如何使用boost::graph算法与listS,setS作为顶点/边缘容器