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

how to use yaml-cpp to modify a map node (delete key's value)

以这个yaml节点为例:

- flow:
    - do:
        ? command:
            - command1: command
            - command2: command
            - command3: command
          name: nameblock
          descr: descrblock
        : block_1

对于值"block_1",key是一个map节点。如何使用yaml-cpp删除最里面的值"block_1",让整个节点变成:

- flow:
    - do:
          command:
            - command1: command
            - command2: command
            - command3: command
          name: nameblock
          descr: descrblock

有什么建议吗?赞赏!

您基本上必须重新分配整个包含节点,而不是删除任何内容。例如:

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;