Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the name of a node?

Tags:

yaml-cpp

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

Fruits:
   - apple
   - orange
   - pear

and in C++:

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

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!

like image 444
user3416593 Avatar asked Nov 10 '22 10:11

user3416593


1 Answers

What you're really looking for is the key associated with a value in a map. You're right, there's no link back from a value to its key, but you can store one, when you're navigating the node in the first place.

If all of your keys are string keys, then whenever you pass a value to some function, just pass along the string key as well:

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

// Call:
doSomething(key, node[key]);
like image 82
Jesse Beder Avatar answered Dec 20 '22 14:12

Jesse Beder