如何使用yaml-cpp修改map节点(删除key的值)
Posted
技术标签:
【中文标题】如何使用yaml-cpp修改map节点(删除key的值)【英文标题】:how to use yaml-cpp to modify a map node (delete key's value) 【发布时间】:2017-10-10 07:10:53 【问题描述】:以这个yaml节点为例:
- flow:
- do:
? command:
- command1: command
- command2: command
- command3: command
name: nameblock
descr: descrblock
: block_1
对于值“block_1”,键是地图节点。怎么用yaml-cpp删除最里面的值“block_1”,让整个节点变成:
- flow:
- do:
command:
- command1: command
- command2: command
- command3: command
name: nameblock
descr: descrblock
有什么建议吗?赞赏!
【问题讨论】:
【参考方案1】:您基本上必须重新分配整个包含节点,而不是删除任何内容。例如:
YAML::Node root = YAML::LoadFile("test.yaml");
// this is now the value of the "do" node
// explanation of each value:
// root[0] - zeroth entry in the top-level sequence
// ["flow"] - value for the key "flow"
// [0] - zeroth entry in the resulting sequence
// ["do"] - value for the key "do"
YAML::Node node = root[0]["flow"][0]["do"];
// we're assuming there's only one entry in the map
// if you want a particular one, you can hunt for it
assert(node.size() == 1);
// this is the key of the first key/value pair
YAML::Node key = node.begin()->first;
// update the whole key/value pair to be just the key
node = key;
【讨论】:
以上是关于如何使用yaml-cpp修改map节点(删除key的值)的主要内容,如果未能解决你的问题,请参考以下文章