ad_ad
ad_ad

Reputation: 385

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?

Upvotes: 1

Views: 281

Answers (1)

sehe
sehe

Reputation: 393114

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"));
    

Upvotes: 1

Related Questions