Search code examples
c++boostboost-propertytree

Adding parent name to boost ptree


I have a boost ptree with nodes:

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

I would like the extract a tree that has "a.b" and "a.c" (but not "b.g"). When I use pt.get_child("a") I get a tree with "b" and "c". Is there a way to do this?


Solution

  • What you describe already works. See it Live On Coliru

    If you want to filter out anything "non-a", just

    • delete the other nodes Live on Coliru

      for (auto it = pt.begin(); it != pt.end();)
      {
          if (it->first != "a")
              it = pt.erase(it);
          else
              ++it;
      }
      
    • create a new tree Live on Coliru

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