Boost Graph 库中的 read_graphml 函数遇到困难

问题描述 投票:0回答:1

我正在尝试使用 Boost 库中的

read_graphml
函数来读取我正在开发的 C++ 应用程序中的一些 .graphml 文件。然而,我在阅读属性时遇到了意想不到的困难。这是我的代码的 MWE:

#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/property_map/dynamic_property_map.hpp>

using namespace boost;

int main(int, char**){

    typedef adjacency_list<vecS, setS, undirectedS> Graph;


    std::ifstream graph_file;
    graph_file.open("more_exampleHoles_8.graphml", std::ifstream::in);

    Graph g;
    boost::dynamic_properties dp;
    boost::read_graphml(graph_file, g, dp);
}

以下是

more_exampleHoles_8.graphml
的内容:

<?xml version='1.0' encoding='UTF-8'?>
<!-- created by IGraph/M, http://szhorvat.net/mathematica/IGraphM -->
<graphml xmlns='http://graphml.graphdrawing.org/xmlns'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:schemaLocation='http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'>
 <key id="v_betti"
     for="node"
     attr.name="betti"
     attr.type="long" />
 <key for='node'
     id='v_dimk'
     attr.name='dimk'
     attr.type='long' />
 <key for='node'
     id='v_gap'
     attr.name='gap'
     attr.type='double' />
 <graph id='Graph'
     edgedefault='undirected'>
  <node id="1">
   <data key="v_betti">5</data>
   <data key='v_dimk'>1</data>
   <data key='v_gap'>0.12405641442417092</data>
  </node>
  <node id='2' />
  <node id='3' />
  <node id='4' />
  <node id='5' />
  <node id='6' />
  <node id='7' />
  <node id='8' />
  <node id='9' />
  <node id='10' />
  <node id='11' />
  <node id='12' />
  <edge source='1'
      target='2' />
  <edge source='1'
      target='3' />
  <edge source='1'
      target='4' />
  <edge source='1'
      target='8' />
  <edge source='2'
      target='7' />
  <edge source='2'
      target='9' />
  <edge source='2'
      target='11' />
  <edge source='3'
      target='5' />
  <edge source='3'
      target='10' />
  <edge source='4'
      target='5' />
  <edge source='4'
      target='6' />
  <edge source='4'
      target='7' />
  <edge source='4'
      target='8' />
  <edge source='4'
      target='9' />
  <edge source='4'
      target='11' />
  <edge source='5'
      target='9' />
  <edge source='5'
      target='10' />
  <edge source='6'
      target='8' />
  <edge source='7'
      target='10' />
  <edge source='8'
      target='10' />
  <edge source='9'
      target='11' />
 </graph>
</graphml>

运行代码时,我收到以下输出:

terminate called after throwing an instance of 'boost::wrapexcept<boost::property_not_found>'
  what():  Property not found: betti.
Aborted (core dumped)

我非常有信心 graphml 的格式良好,所以在我看来这是 read_graphml 函数的一个错误。如果我从 .graphml 文件中删除以下行,一切都会正常,但我当然不想删除这些行。

   <data key="v_betti">5</data>
   <data key='v_dimk'>1</data>
   <data key='v_gap'>0.12405641442417092</data>
c++ boost graphml
1个回答
0
投票

Boost 希望您为所有存在的属性定义属性映射。如果您想忽略未知属性,请将过滤器添加到动态属性构造函数中: boost::dynamic_properties dp(boost::ignore_other_properties);

© www.soinside.com 2019 - 2024. All rights reserved.