我已经阅读了boost::property_tree文档,但没有找到更新或合并ptree的方法。我该怎么做?
在下面的代码中,如何编写update_ptree函数?
#include <iostream>
#include <boost/property_tree/ptree.hpp>
using boost::property_tree::ptree;
class A
{
ptree pt_;
public:
void set_ptree(const ptree &pt)
{
pt_ = pt;
};
void update_ptree(const ptree &pt)
{
//How do I merge/update a ptree?
};
ptree get_ptree()
{
return pt_;
};
};
int main()
{
A a;
ptree pt;
pt.put<int>("first.number",0);
pt.put<int>("second.number",1);
pt.put<int>("third.number",2);
a.set_ptree(pt);
ptree pta = a.get_ptree();
//prints "0 1 2"
std::cout << pta.get<int>("first.number") << " "
<< pta.get<int>("second.number") << " "
<< pta.get<int>("third.number") << "\n";
ptree updates;
updates.put<int>("first.number",7);
a.update_ptree(updates);
pta = a.get_ptree();
//Because the update_tree function doesn't do anything it just prints "0 1 2".
//I would like to see "7 1 2"
std::cout << pta.get<int>("first.number") << " "
<< pta.get<int>("second.number") << " "
<< pta.get<int>("third.number") << "\n";
return 0;
}
我考虑过遍历新的ptree并使用“put”插入值。但是,“put”需要一个类型,我不知道如何从新的ptree中获取该信息并将其用作旧ptree的参数。
在update_ptree函数中,我尝试过使用以下方法:
pt_.add_child(".",pt);
我试图将pt添加为pt_根的子级,但不幸的是似乎无法实现。
有什么想法吗?
非常感谢任何帮助。
谢谢。
(我尝试将tags property_tree和ptree添加到这个问题中,但被禁止了)
ptree::path_type curPath = childPath / ptree::path_type(it->first);
我在文档中没有看到这个操作符的定义。 - 2NinerRomeo