나는 boost :: property_tree에 대한 문서를 읽었으며 ptree를 다른 ptree로 업데이트하거나 병합하는 방법을 찾지 못했습니다. 어떻게해야합니까?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를 추가하려고합니다. 불행히도 이것은 작동하지 않는 것 같습니다.
아이디어가 있으십니까?
도움에 감사드립니다.
감사합니다.
는 (이 질문에 태그 property_tree 및 ptree을 추가하려고하지만 허용되지 않았다)
감사합니다. 이것은 흥미로운 해결책입니다. 볼 컴파일! 그러나 "같은 경로의 여러 노드를 가질 수 있습니다"라는 것은 무엇을 의미합니까? Tree_1 = "a.b.c"= 0 업데이트 트리 Tree_2 = "a.b.c"= 1, "a.b.d"= 2. "a.b.d"= 2 만 업데이트됩니까? (디버그하고 보게 될 것이다) – mantler
정확히 같은 경로를 가진 노드를 두 개 이상 가질 수있다. Tree_1에 "a.b.c"= 1, "a.b.c"= 2, Tree_2에 "a.b.c"= 1이 있으면 Tree_1을 Tree_2로 업데이트 한 후 Tree_2에 "a.b.c"= 2가 포함됩니다. –
이것은 정말 멋진 코드입니다. 이 연산자가 path_type에서 작동한다는 것을 어떻게 알았습니까? :'ptree :: path_type curPath = childPath/ptree :: path_type (it-> first);'오퍼레이터가 문서에 정의되어 있는지 확인할 수 없습니다. – 2NinerRomeo