In C++ using ptree
from boost
, I need to find the relative key to access a.b.c2.e1
from a.b
. This key is c2.e1
. How can I write a function which finds this relative key?
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace boost::property_tree;
std::string relative_key(const ptree &p1,const ptree &p2)
{
??????????????
// return "b.c2.e1";
}
int main()
{
ptree pt0;
pt0.put("a.b.c1",4);
pt0.put("a.b.c2.e1",4);
pt0.put("a.b.c4",4);
pt0.put("a.d",4);
pt0.put("k.m",4);
pt0.put("k.n",4);
ptree &pt_e1=pt0.get_child("a.b.c2.e1");
ptree &pt_b=pt0.get_child("a.b");
std::cout<<relative_key(pt_e1,pt_b)<<std::endl;
return 0;
}
You'd need to write a recursive search function, like:
Use it like:
Live On Coliru
Which prints "c2.e1".
Full Listing
Live On Coliru