【问题标题】:how to use yaml-cpp to modify a map node (delete key's value)如何使用yaml-cpp修改map节点(删除key的值)
【发布时间】: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

有什么建议吗?赞赏!

【问题讨论】:

    标签: nodes yaml-cpp


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多