How can I get the name of a node?

use*_*593 5 yaml-cpp

Is there a way to get the name of a node? For example:

Fruits:
   - apple
   - orange
   - pear
Run Code Online (Sandbox Code Playgroud)

and in C++:

YAML::Node fruit = parser["Fruits"];
cout << fruit.name() << endl;  // should print "Fruits"
Run Code Online (Sandbox Code Playgroud)

is there something like YAML::Node::name()? I don't see anything in Node.h that fits the bill.

If there isn't, any suggestions on a simple way to modify the code to record this information?

Thanks!

Jes*_*der 2

您真正要寻找的是与映射中的值关联的键。你是对的,没有从值到它的键的链接,但是当你首先导航节点时,你可以存储一个链接。

如果所有键都是字符串键,那么每当您将值传递给某个函数时,也只需传递字符串键:

// Instead of:
doSomething(node[key]);

// Call:
doSomething(key, node[key]);
Run Code Online (Sandbox Code Playgroud)

  • 对于解析器来说,如果能将这些信息嵌入其中,那就太好了,这样用户代码就不必为了错误捕获的目的而跟踪层次结构。 (3认同)
  • 如果解析器不保留此信息,那么我必须使用我自己的错误处理或其他类似的侵入性错误捕获将大多数 YAML 调用嵌入到 try/catch 块中。这给用户代码增加了大量的开销。 (2认同)