C++ Boost graph:如何在不复制属性的情况下复制图的节点和边?
我正在使用具有捆绑属性的boost图。在我构建第一个引用树之后。我想有几个其他树具有相同的结构和层次结构,但不同的顶点和边属性。我发现有一种复制图的方法,但不知道如何使用它来达到我的目的。 例如,我首先创建一个引用树,C++ Boost graph:如何在不复制属性的情况下复制图的节点和边?,c++,boost,graph,C++,Boost,Graph,我正在使用具有捆绑属性的boost图。在我构建第一个引用树之后。我想有几个其他树具有相同的结构和层次结构,但不同的顶点和边属性。我发现有一种复制图的方法,但不知道如何使用它来达到我的目的。 例如,我首先创建一个引用树,VertexProperty1和EdgeProperty1是绑定的属性 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty1, EdgePr
VertexProperty1
和EdgeProperty1
是绑定的属性
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty1, EdgeProperty1> Graph;
Graph g1;
typedef boost::邻接列表图;
图g1;
经过一些处理后,g1包含一些顶点和边。
然后我想要一个具有不同绑定属性的复制树
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty2, EdgeProperty2> Graph2;
copy_graph(g1, g2, ???);
typedef boost::邻接列表图2;
复制图(g1,g2,?);
提前感谢您的帮助。首选示例代码。如果查看,您可以看到参数顶点复制
和边复制
是实际复制属性的参数。这些参数的默认值复制每个顶点/边中的所有属性,您需要的是“不做任何事情”的属性:
#包括
#包括
#包括
#包括
#包括
结构VertexProp1
{
内色;
};
结构VertexProp2
{
std::字符串名;
};
结构EdgeProp1
{
双倍重量;
};
结构EdgeProp2
{
std::字符串名;
};
typedef boost::邻接列表图1;
typedef boost::graph_traits::vertex_描述符VertexDesc;
typedef boost::邻接列表图2;
结构不做任何事
{
模板
void运算符()(常量VertexOrEdge1&,VertexOrEdge2&)常量
{
}
};
无效构建图(图1和图g)
{
VertexDesc v0=添加_顶点(VertexProp1{1},g);
VertexDesc v1=添加_顶点(VertexProp1{2},g);
VertexDesc v2=添加_顶点(VertexProp1{3},g);
添加_边(v0,v1,EdgeProp1{1.0},g);
添加_边(v1,v2,EdgeProp1{2.0},g);
添加_边(v2,v0,EdgeProp1{3.0},g);
}
int main()
{
图1 g1;
构建图(g1);
性病::真不错,你赢了我一拳。而且好多了:)
struct do_nothing
{
template <typename VertexOrEdge1, typename VertexOrEdge2>
void operator()(const VertexOrEdge1& , VertexOrEdge2& ) const
{
}
};
copy_graph(g1,g2,boost::vertex_copy(do_nothing()).edge_copy(do_nothing()));
#include <iostream>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>
#include <boost/graph/graph_utility.hpp>
struct VertexProp1
{
int color;
};
struct VertexProp2
{
std::string name;
};
struct EdgeProp1
{
double weight;
};
struct EdgeProp2
{
std::string name;
};
typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS,VertexProp1,EdgeProp1> Graph1;
typedef boost::graph_traits<Graph1>::vertex_descriptor VertexDesc;
typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS,VertexProp2,EdgeProp2> Graph2;
struct do_nothing
{
template <typename VertexOrEdge1, typename VertexOrEdge2>
void operator()(const VertexOrEdge1& , VertexOrEdge2& ) const
{
}
};
void build_graph(Graph1& g)
{
VertexDesc v0=add_vertex(VertexProp1{1},g);
VertexDesc v1=add_vertex(VertexProp1{2},g);
VertexDesc v2=add_vertex(VertexProp1{3},g);
add_edge(v0,v1,EdgeProp1{1.0},g);
add_edge(v1,v2,EdgeProp1{2.0},g);
add_edge(v2,v0,EdgeProp1{3.0},g);
}
int main()
{
Graph1 g1;
build_graph(g1);
std::cout << "Graph1" << std::endl;
print_graph(g1);
Graph2 g2;
copy_graph(g1,g2,boost::vertex_copy(do_nothing()).edge_copy(do_nothing()));
std::cout << "Graph2" << std::endl;
print_graph(g2);
}