私は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