26

我知道正在接近提升属性树,并看到它是用于 c++ 编程的提升库的一个很好的特性。

好吧,我有一个疑问?如何使用迭代器或类似方法迭代属性树?

在参考中,只有一个浏览树的示例:

BOOST_FOREACH

但是没有别的了吗?类似于 stl 的容器之类的东西?谈到代码质量,这将是一个更好的解决方案......

4

5 回答 5

32

这是我经过大量实验后得出的结论。我想在社区中分享它,因为我找不到我想要的东西。每个人似乎都只是发布了来自 boost 文档的答案,我发现这还不够。无论如何:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <string>
#include <iostream>

using namespace std; 
using boost::property_tree::ptree; 

string indent(int level) {
  string s; 
  for (int i=0; i<level; i++) s += "  ";
  return s; 
} 

void printTree (ptree &pt, int level) {
  if (pt.empty()) {
    cerr << "\""<< pt.data()<< "\"";
  }

  else {
    if (level) cerr << endl; 

    cerr << indent(level) << "{" << endl;     

    for (ptree::iterator pos = pt.begin(); pos != pt.end();) {
      cerr << indent(level+1) << "\"" << pos->first << "\": "; 

      printTree(pos->second, level + 1); 
      ++pos; 
      if (pos != pt.end()) {
        cerr << ","; 
      }
      cerr << endl;
    } 

   cerr << indent(level) << " }";     
  }

  return; 
}

int main(int, char*[]) {

  // first, make a json file:
  string tagfile = "testing2.pt"; 
  ptree pt1;
  pt1.put("object1.type","ASCII");  
  pt1.put("object2.type","INT64");  
  pt1.put("object3.type","DOUBLE");  
  pt1.put("object1.value","one");  
  pt1.put("object2.value","2");  
  pt1.put("object3.value","3.0");  
  write_json(tagfile, pt1); 

  ptree pt;
  bool success = true; 

  try {
      read_json(tagfile, pt); 
      printTree(pt, 0); 
      cerr << endl; 
  }catch(const json_parser_error &jpe){
      //do error handling
      success = false
  }

  return success; 
}

这是输出:

rcook@rzbeast (blockbuster): a.out
{
  "object1": 
  {
    "type": "ASCII",
    "value": "one"
   },
  "object2": 
  {
    "type": "INT64",
    "value": "2"
   },
  "object3": 
  {
    "type": "DOUBLE",
    "value": "3.0"
   }
 }
rcook@rzbeast (blockbuster): cat testing2.pt 
{
    "object1":
    {
        "type": "ASCII",
        "value": "one"
    },
    "object2":
    {
        "type": "INT64",
        "value": "2"
    },
    "object3":
    {
        "type": "DOUBLE",
        "value": "3.0"
    }
}
于 2013-06-20T17:12:04.147 回答
18

BOOST_FOREACH 只是一种方便的迭代方式,可以通过迭代器、begin() 和 end() 来完成

Your_tree_type::const_iterator end = tree.end();
for (your_tree_type::const_iterator it = tree.begin(); it != end; ++it)
    ...

从 C++11 开始,它是:

for (auto& it: tree)
    ...
于 2011-01-03T17:45:54.350 回答
9

我最近遇到了这个问题,发现答案不适合我的需要,所以我想出了这个简短而甜蜜的片段:

using boost::property_tree::ptree;

void parse_tree(const ptree& pt, std::string key)
{
  std::string nkey;

  if (!key.empty())
  {
    // The full-key/value pair for this node is
    // key / pt.data()
    // So do with it what you need
    nkey = key + ".";  // More work is involved if you use a different path separator
  }

  ptree::const_iterator end = pt.end();
  for (ptree::const_iterator it = pt.begin(); it != end; ++it)
  {
    parse_tree(it->second, nkey + it->first);
  }
}

需要注意的重要一点是,除了根节点之外的任何节点都可以包含数据以及子节点。该if (!key.empty())位将获取除根节点以外的所有节点的数据,如果有的话,我们还可以开始构建节点子节点的循环路径。

您将通过调用开始解析parse_tree(root_node, ""),当然您需要在此函数中做一些事情以使其值得做。

如果您在不需要完整路径的地方进行一些解析,只需删除nkey变量及其操作,然后传递it->first给递归函数即可。

于 2014-09-01T20:30:31.767 回答
2

答案的补充如何迭代提升属性树?

在基于 for 的 C++11 样式范围中for (auto node : tree),每个node都是std::pair<key_type, property_tree>

而在手动编写的迭代中

Your_tree_type::const_iterator end = tree.end();
for (your_tree_type::const_iterator it = tree.begin(); it != end; ++it)
...

迭代器it是指向这样一对的指针。用法上的差别很小。例如,要访问密钥,可以写it->firstbut node.first

作为新答案发布,因为我对原始答案的建议编辑被拒绝,并建议发布新答案。

于 2019-03-07T00:25:53.927 回答
-1

基于 BFS 的打印 ptree 遍历,如果我们想做一些算法操作,可以使用

int print_ptree_bfs(ptree &tree) {
try {
    std::queue<ptree*> treeQ;
    std::queue<string> strQ;

    ptree* temp;

    if (tree.empty())
        cout << "\"" << tree.data() << "\"";

    treeQ.push(&tree);
    //cout << tree.data();
    strQ.push(tree.data());

    while (!treeQ.empty()) {
        temp = treeQ.front();
        treeQ.pop();

        if (temp == NULL) {
            cout << "Some thing is wrong" << std::endl;
            break;
        }
        cout << "----- " << strQ.front() << "----- " << std::endl;
        strQ.pop();

        for (auto itr = temp->begin(); itr != temp->end(); itr++) {
            if (!itr->second.empty()) {
                //cout << itr->first << std::endl;
                treeQ.push(&itr->second);
                strQ.push(itr->first);
            } else {
                cout<<itr->first << " " << itr->second.data() << std::endl;
            }
        }

        cout << std::endl;

     }
   } catch (std::exception const& ex) {
    cout << ex.what() << std::endl;
   }
   return EXIT_SUCCESS;
  }
于 2018-08-07T18:16:56.980 回答