C++ 从boost::labeled_图获取节点标签
我想在BGL的标签图中检索标签节点的标签,但找不到执行此操作的方法 以下MWE展示了我所寻找的:C++ 从boost::labeled_图获取节点标签,c++,boost,boost-graph,C++,Boost,Boost Graph,我想在BGL的标签图中检索标签节点的标签,但找不到执行此操作的方法 以下MWE展示了我所寻找的: //g++ -O3 question.cpp -o question.exe -I. --std=c++11 -lprotobuf-lite -lpthread -lz -losmpbf #include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/labeled
//g++ -O3 question.cpp -o question.exe -I. --std=c++11 -lprotobuf-lite -lpthread -lz -losmpbf
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
typedef long long node_id_t;
typedef boost::adjacency_list<
boost::listS, // Store out-edges of each vertex in a std::list
boost::listS, // Store vertex set in a std::list
boost::bidirectionalS, // The file dependency graph is directed
boost::no_property, // vertex properties
boost::no_property // edge properties
> AdjGraph;
typedef boost::labeled_graph<
AdjGraph,
node_id_t // Node ID
> LabeledGraph;
int main(){
LabeledGraph g;
add_vertex( 10, g );
add_vertex( 20, g );
add_vertex( 30, g );
add_vertex( 40, g );
add_vertex( 50, g );
boost::add_edge_by_label(10,30,g);
boost::add_edge_by_label(10,50,g);
boost::add_edge_by_label(30,40,g);
boost::add_edge_by_label(50,20,g);
BGL_FORALL_EDGES(e, g, LabeledGraph){
auto source_n = boost::source(e,g);
//How do I do the following?
//std::cerr<<boost::get_label_of_vertex(source_n)<<std::endl;
}
}
//g++-O3 question.cpp-o question.exe-I.--std=c++11-lprotobuf lite-lpthread-lz-losmpbf
#包括
#包括
#包括
#包括
typedef long long node\u id\t;
typedef boost::邻接列表<
boost::list,//在std::list中存储每个顶点的边
boost::list,//将顶点集存储在std::list中
boost::bidirectionalS,//文件依赖关系图是定向的
boost::no_属性,//顶点属性
boost::no_属性//边缘属性
>调整图;
typedef boost::标记的_图<
AdjGraph,
节点\u id\u t//节点id
>标签图;
int main(){
标签图g;
添加_顶点(10,g);
添加_顶点(20,g);
添加_顶点(30,g);
添加_顶点(40,g);
添加_顶点(50,g);
boost::按标签(10,30,g)添加边;
boost::按标签添加边(10,50,g);
boost::按标签(30,40,g)添加边;
boost::按标签(50,20,g)添加边;
所有边缘的BGL_(例如,LabeledGraph){
自动源=boost::源(e,g);
//我该如何做以下工作?
//啊,我发现你的问题顺序不对:)
我不认为labeled\u graph
adapter具有此功能。相反,就像在我的文章中一样,我建议将标签存储为顶点属性(捆绑或顶点索引)
捆绑
// g++ -O3 question.cpp -o question.exe -I. --std=c++11 -lprotobuf-lite -lpthread -lz -losmpbf
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
struct MyVertex {
long long label;
MyVertex(long long label = 0) : label(label) {}
};
typedef boost::adjacency_list<boost::listS, // Store out-edges of each vertex in a std::list
boost::listS, // Store vertex set in a std::list
boost::bidirectionalS, // The file dependency graph is directed
MyVertex, // vertex properties
boost::no_property // edge properties
> AdjGraph;
int main() {
AdjGraph g;
std::map<size_t, AdjGraph::vertex_descriptor> vertex_map;
for (int i = 1; i<6; ++i)
vertex_map.emplace(10*i, add_vertex(10*i, g));
boost::add_edge(vertex_map[10], vertex_map[30], g);
boost::add_edge(vertex_map[10], vertex_map[50], g);
boost::add_edge(vertex_map[30], vertex_map[40], g);
boost::add_edge(vertex_map[50], vertex_map[20], g);
BGL_FORALL_EDGES(e, g, AdjGraph) {
auto source_n = boost::source(e, g);
std::cerr << g[source_n].label << "\n";
}
}
// g++ -O3 question.cpp -o question.exe -I. --std=c++11 -lprotobuf-lite -lpthread -lz -losmpbf
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
typedef boost::adjacency_list<boost::listS, // Store out-edges of each vertex in a std::list
boost::listS, // Store vertex set in a std::list
boost::bidirectionalS, // The file dependency graph is directed
boost::property<boost::vertex_index_t, size_t>, // vertex properties
boost::no_property // edge properties
> AdjGraph;
int main() {
AdjGraph g;
std::map<size_t, AdjGraph::vertex_descriptor> vertex_map;
for (int i = 1; i<6; ++i)
vertex_map.emplace(10*i, add_vertex(10*i, g));
boost::add_edge(vertex_map[10], vertex_map[30], g);
boost::add_edge(vertex_map[10], vertex_map[50], g);
boost::add_edge(vertex_map[30], vertex_map[40], g);
boost::add_edge(vertex_map[50], vertex_map[20], g);
auto index = boost::get(boost::vertex_index, g);
BGL_FORALL_EDGES(e, g, AdjGraph) {
auto source_n = boost::source(e, g);
std::cerr << boost::get(index, source_n) << "\n";
}
}