使用 boost 将无平行边图保存到 graphml 文件
Saving no-parallel edge graph to a graphml file using boost
以下代码将图形保存到 graphml 文件中。它有效!.但是当我用typedef adjacency_list< setS, setS, directedS,
替换typedef adjacency_list< vecS, vecS, directedS,
以便没有重复的顶点或边插入图形时,它会抱怨。
#include <boost/graph/graphml.hpp>
using namespace std;
typedef struct {
string name;
string label;
} vertex_type_t;
int main(int,char*[])
{
using namespace boost;
typedef adjacency_list< vecS, vecS, directedS,
vertex_type_t > graph_t;
graph_t g;
graph_t::vertex_descriptor v1 = add_vertex(g);
graph_t::vertex_descriptor v2 = add_vertex(g);
dynamic_properties dp;
dp.property("name", get(&vertex_type_t::name, g));
write_graphml(std::cout, g, dp, true);
return 0;
}
我不能从错误中得到太多。我相信以下是主要错误。
/usr/include/boost/graph/graphml.hpp: In function ‘void boost::write_graphml(std::ostream&, const Graph&, VertexIndexMap, const boost::dynamic_properties&, bool) [with Graph = boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, VertexIndexMap = boost::adj_list_vertex_property_map<boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t>, std::ostream = std::basic_ostream<char>]’:
/usr/include/boost/graph/graphml.hpp:345:5: instantiated from ‘void boost::write_graphml(std::ostream&, const Graph&, const boost::dynamic_properties&, bool) [with Graph = boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, std::ostream = std::basic_ostream<char>]’
write_graphviz.cpp:24:39: instantiated from here
/usr/include/boost/graph/graphml.hpp:301:9: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & out), ((const char*)" <node id="n")) << boost::get [with PropertyMap = boost::adj_list_vertex_property_map<boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t>, Reference = const boost::detail::error_property_not_found&, K = void*]((*(const boost::put_get_helper<const boost::detail::error_property_not_found&, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >*)(& vertex_index)), (* & v.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = void*, std::_Rb_tree_const_iterator<_Tp>::reference = void* const&]()))’
/usr/include/boost/graph/graphml.hpp:301:9: note: candidates are:
/usr/include/c++/4.6/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:110:7: note: no known conversion for argument 1 from ‘const boost::detail::error_property_not_found’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.6/ostream:119:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
我真的很想使用 setS 作为边缘容器。我不确定如何修改此程序以使其开始工作,因为错误消息对我来说没有多大意义。
问题是write_graphml
需要在图形中创建一个VertexIndexMap,并且默认情况下只有adjacency_list
VertexList=vecS
创建一个。如果您使用listS
或setS
,则必须自己创建一个。
我真的很想使用 setS 作为边缘容器。
如果您只需要在OutEdgeList
中使用setS
,则只需使用:
typedef adjacency_list< setS, vecS, directedS,
vertex_type_t > graph_t;
如果您必须同时使用setS
,那么您的程序应该是(基于此答案):
#include <boost/graph/graphml.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <map>
using namespace std;
typedef struct
{
string name;
string label;
} vertex_type_t;
int main ( int, char*[] )
{
using namespace boost;
typedef adjacency_list < setS, vecS, directedS,
vertex_type_t > graph_t;
typedef graph_t::vertex_descriptor NodeID; //define your Vertex Index Map
typedef std::map<NodeID, size_t> IndexMap;
IndexMap mapIndex;
boost::associative_property_map<IndexMap> propmapIndex ( mapIndex );
graph_t g;
graph_t::vertex_descriptor v1 = add_vertex ( g );
graph_t::vertex_descriptor v2 = add_vertex ( g );
int i = 0; //fill your Vertex Index Map
BGL_FORALL_VERTICES ( v, g, graph_t )
{
put ( propmapIndex, v, i++ );
}
g[v1].name="FirstVertex";
g[v2].name="SecondVertex";
dynamic_properties dp;
dp.property ( "name", get ( &vertex_type_t::name, g ) );
write_graphml ( std::cout, g, propmapIndex, dp, true );
return 0;
}
我的第一个想法是,新的图形类型(带有setS)不满足write_graphml(..)函数的要求:http://www.boost.org/doc/libs/1_51_0/libs/graph/doc/write_graphml.html
它指出"图形的类型必须是VertexListGraph的模型",它在这里描述: http://www.boost.org/doc/libs/1_51_0/libs/graph/doc/VertexListGraph.html
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 哈希文件递归并保存到矢量Cryptopp中
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 无法通过头文件保存变量的值
- 我们可以将数据永久保存为数据结构吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- 如何访问由共享指针保存的类方法?
- 为什么签名字符可以保存大于 127 的值?
- 使用 boost 将无平行边图保存到 graphml 文件