如何使用yaml-cpp修改地图节点(删除键的值)

时间:2017-10-10 07:10:54

标签: nodes yaml-cpp

以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 个答案:

答案 0 :(得分: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;