C++11 使用BGL从向量填充树
我有两个物体的向量,我需要用它们做一个树结构。让我们假设我们有C++11 使用BGL从向量填充树,c++11,vector,tree,boost-graph,C++11,Vector,Tree,Boost Graph,我有两个物体的向量,我需要用它们做一个树结构。让我们假设我们有向量父母和向量叶子。因此,vector parents的每个元素在树的末尾都有几个叶子。我要做的是如下定义顶点属性和边属性,然后定义一个双向图: struct VertexData { std::string obj_name; // concatenation of labels std::string obj_class_num; int num; vector <int> segmen
向量父母
和向量叶子
。因此,vector parents
的每个元素在树的末尾都有几个叶子。我要做的是如下定义顶点属性
和边属性
,然后定义一个双向图
:
struct VertexData
{
std::string obj_name; // concatenation of labels
std::string obj_class_num;
int num;
vector <int> segments_list;
bool is_leaf=false;
};
struct EdgeData
{
std::string edge_name;
double confidence;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS,
boost::bidirectionalS,
VertexData,
boost::property<boost::edge_weight_t, double, EdgeData> > Graph;
Graph graph;
struct VertexData
{
std::string obj_name;//标签的串联
std::字符串对象类编号;
int-num;
向量段列表;
bool是_leaf=false;
};
边缘结构
{
std::字符串边缘\u名称;
双重信心;
};
typedef boost::邻接列表图;
图形;
向量叶
,对于每个成员,我找到父对象并生成一条边。然后将特性指定给边和顶点。但对于下一个叶子,我应该检查它在树中是否已经有一个父节点,或者我应该为它的父节点添加一个新的顶点向量父元素
,并尝试为每个元素生成其叶子。但我不确定正确的方法是什么。
以下是一个链接:
我试着做同样的事情,但是迭代vector <class1> parents; // this has some objects of type class1
vector <class2> leaves; // this has some objects of type class2
/// declare the graph
typedef boost::adjacency_list<boost::vecS, boost::vecS,
boost::bidirectionalS,
VertexData,
boost::property<boost::edge_weight_t, double, EdgeData> > Graph;
/// instantiate the graph
Graph graph;
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<Graph>::edge_descriptor edge_t;
vector<vertex_t> obj_vertices;
vector<string> parents_labels_v;
bool parent_exist=false;
/// loop through leaves and make edges with associated parent
for (auto leaf: leaves) {
int leaf_nr = leaf.Number;
vertex_t v = boost::add_vertex(graph); // this is the leaf vertex
graph[v].num = leaf_nr; // leaf number
graph[v].is_leaf = true;
/// access the parent label by leaf number
string label1 = parents[leaf_nr].label;
/// check if the parent already exist, using its label
if(std::find(parents_labels_v.begin(), parents_labels_v.end(), label1)
!= parents_labels_v.end()){
parent_exist = true;
}else{
parents_labels_v.push_back(label1);
}
if(parent_exist) {
// find already_exist parent vertex to make the edge
vertex_t u = ??? here i have problem
// Create an edge connecting those two vertices
edge_t e; bool b;
boost::tie(e,b) = boost::add_edge(u,v,graph);
} else{
// if parent-vertex there is not, add it to the graph
vertex_t u = boost::add_vertex(graph); // this is the parent vertex
graph[u].obj_name = label1;
graph[u].segments_list.push_back(leaf_nr);
obj_vertices.push_back(u);
// Create an edge connecting those two vertices
edge_t e; bool b;
boost::tie(e,b) = boost::add_edge(u,v,graph);
}
}
向量父项;//这有一些class1类型的对象
向量叶;//这有一些类型为class2的对象
///声明图形
typedef boost::邻接列表图;
///实例化图形
图形;
typedef boost::graph_traits::vertex_描述符vertex_t;
typedef boost::graph_traits::edge_描述符edge_t;
向量对象顶点;
向量父母标签;
bool parent_exist=false;
///循环穿过树叶,并与关联的父对象形成边
用于(自动叶:叶){
int leaf_nr=叶编号;
顶点_tv=boost::添加_顶点(图);//这是叶顶点
图[v].num=leaf\u nr;//叶数
图[v]。是_leaf=true;
///按叶编号访问父标签
字符串label1=父项[leaf\u nr]。标签;
///使用父项的标签检查父项是否已存在
if(std::find(父项标签开始(),父项标签结束(),标签1)
!=父项\u标签\u v.end(){
parent_exist=true;
}否则{
父母的标签推后(标签1);
}
如果(父项不存在){
//查找已存在的父顶点以生成边
vertex_t u=???这里有个问题
//创建连接这两个顶点的边
边缘;
boost::tie(e,b)=boost::add_边(u,v,图);
}否则{
//如果没有父顶点,请将其添加到图形中
顶点(图);//这是父顶点
图[u].obj_name=label1;
图[u]。段列表。向后推(叶编号);
对象顶点。向后推(u);
//创建连接这两个顶点的边
边缘;
boost::tie(e,b)=boost::add_边(u,v,图);
}
}
能否用代码替换散文描述。该描述以多种方式混淆了“成员”/“元素”和“子元素”。另外,在试图理解你的描述之后,我认为“我有3个向量”甚至都不是真的。我编辑了这个问题,使它更简单明了。然而,代码只是测试,我不确定这是否是一种优雅的方法。我认为第二种方法更有意义,但我不知道如何迭代添加左顶点。你能用代码替换散文描述吗。该描述以多种方式混淆了“成员”/“元素”和“子元素”。另外,在试图理解你的描述之后,我认为“我有3个向量”甚至都不是真的。我编辑了这个问题,使它更简单明了。然而,代码只是测试,我不确定这是否是一种优雅的方法。我认为第二种方法更有意义,但我不知道如何迭代添加左顶点。