在yaml-cpp中按名称删除节点

时间:2017-02-25 08:41:00

标签: c++ yaml yaml-cpp

我似乎无法使用YAML-cpp库管理从YAML节点中删除子节点。这是我尝试的代码:

is

这是输出:

  YAML::Node y = YAML::Load("\
  a: first\n\
  b: second\n\
  c: \n\
      d: third\n\
      e: \n\
          f: fourth\n\
          g: fifth\n\
  ");  
  cout << y;
  cout << endl << endl;
  y.remove(y["b"]);
  cout << y;
  cout << endl;

虽然预期输出应该是第二个发出的YAML流不包含&#34; b&#34;元件。

我在这里缺少什么?谢谢:))

1 个答案:

答案 0 :(得分:1)

结果发现node.remove()在yaml-cpp 0.5.2中被破坏,这是Ubuntu Xenial和许多其他发行版的当前版本。 (https://github.com/jbeder/yaml-cpp/issues/338

代码应如下所示:

YAML::Node y = YAML::Load("\
a: first\n\
b: second\n\
c: \n\
  d: third\n\
  e: \n\
      f: fourth\n\
      g: fifth\n\
");  
cout << y;
cout << endl << endl;
y.remove("b");
cout << y;
cout << endl;

所以remove的正确参数是一个字符串(假设我正从地图中删除),原来没有编译:

/usr/include/yaml-cpp/node/impl.h:392:58:   required from ‘bool    YAML::Node::remove(const Key&) [with Key = char [2]]’
/turbine/turbine/src/components/yaml-test.cpp:51:15:   required from here
/usr/include/yaml-cpp/node/detail/impl.h:136:15: error: ‘equals’ was not declared in this scope
 if (equals(*it->first, key, pMemory)) {
...

但在0.5.3中工作正常。

相关问题