You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boost Graph read_graphml基础使用案例问题排查

Boost Graph read_graphml 配置问题

我始终无法正确配置Boost Graph的read_graphml函数。省略基础文件IO操作,我的代码如下:

struct NodeProperties {
  std::string d{};
  std::string e{};
  std::string f{};
};
struct EdgeProperties {};
struct GraphProperties {
  std::string a{};
  std::string b{};
  std::string c{};
};

using DiGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, NodeProperties, EdgeProperties, GraphProperties>;
DiGraph myTree;

std::map<std::string,std::string> attribute_name2name;
//attribute_name2name.insert( make_pair(std::string("a"), std::string("a") ) ); // doesnt help
boost::associative_property_map<std::map<std::string,std::string>> graphname_map( attribute_name2name );

boost::dynamic_properties treeProperties;
treeProperties.property( "a", graphname_map );

try {
  read_graphml(graphmlFile, tree, treeProperties);
}
catch ( std::exception &ex ) {
  std::cerr << "Exception caught from read_graphml: " << ex.what() << "\n";
}
boost::print_graph(tree, "tree");

对应的GraphML文件结构大致如下(手动编写可能存在笔误):

<?xml version="1.0" encoding="UTF-8"?>
<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="key0" for="graph" attr.name="a" attr.type="string" />
  <key id="key1" for="graph" attr.name="b" attr.type="string" />
  <key id="key2" for="graph" attr.name="c" attr.type="string" />
  <key id="key3" for="node" attr.name="d" attr.type="string" />
  <key id="key4" for="node" attr.name="e" attr.type="string" />
  <key id="key5" for="node" attr.name="f" attr.type="string" />
  <graph id="G" edgefault="directed" parse.nodeis="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
    <data key="key0">foo</data>
    <data key="key1">foo</data>
    <data key="key2">foo</data>
      <node id="n0">
        <data key="key3">bar</data>
        <data key="key4">bar</data>
        <data key="key5">bar</data>
      </node>
      <node id="n1">
        <data key="key3">bar</data>
        <data key="key4">bar</data>
        <data key="key5">bar</data>
      </node>
      <node id="n2">
        <data key="key3">bar</data>
        <data key="key4">bar</data>
        <data key="key5">bar</data>
      </node>
      <edge id="e0" source="n0" target="n1"> </edge>
      <edge id="e0" source="n0" target="n2"> </edge>
    </graph>
</graphml>

调用read_graphml时抛出异常:

Property not found: a

但如果将read_graphml的第四个可选参数设置为"G",则无异常抛出,但boost::print_graph无任何输出,连“tree”都不打印。

注:我不需要通用代码,已知节点和图属性固定,可静态写入源码;未使用ignore_other_properties是因为需要读取所有属性,且此处会在文件第一个属性处崩溃。

尝试参考示例实现,但编译器无法解析boost::get(&GraphProperties::a,tree),我认为这种实现方式更优,很多Stack Overflow提问者都使用该方式,但需先解决编译错误。

内容的提问来源于stack exchange,提问作者Maciej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 10:53:12