【问题标题】:Adding parent name to boost ptree添加父名称以提升 ptree
【发布时间】:2014-10-16 02:52:04
【问题描述】:

我有一个带节点的 boost ptree:

pt.put("a.b", 1.0);
pt.put("a.c", 2.0);
pt.put("b.g", 3.0);

我想提取具有“a.b”和“a.c”(但没有“b.g”)的树。当我使用 pt.get_child("a") 时,我得到一棵带有“b”和“c”的树。有没有办法做到这一点?

【问题讨论】:

    标签: c++ boost boost-propertytree


    【解决方案1】:

    您所描述的已经有效。看到它Live On Coliru

    如果你想过滤掉任何“非a”,只需

    • 删除其他节点Live on Coliru

      for (auto it = pt.begin(); it != pt.end();)
      {
          if (it->first != "a")
              it = pt.erase(it);
          else
              ++it;
      }
      
    • 创建一棵新树Live on Coliru

      ptree pt2;
      pt2.add_child("a", pt.get_child("a"));
      

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 2015-12-16
      • 2017-12-28
      • 2017-05-27
      • 2017-03-10
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      相关资源
      最近更新 更多