我知道正在促进属性树的接近,并发现它是C ++编程的Boost Libs的一个很好的功能。

好吧,我有疑问吗?如何使用迭代器或类似的方式迭代属性树?

参考文献中只是一个示例,即通过:

BOOST_FOREACH

但是只有更多吗?类似于STL的容器?谈到代码质量。

有帮助吗?

解决方案

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)
    ...

其他提示

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

#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"
    }
}

我最近遇到了这个问题,发现我的需求不完整的答案,所以我想出了这个简短而甜美的片段:

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()) BIT将获得除根节点以外的所有数据,我们还可以开始为节点的孩子循环构建路径。

您会通过打电话开始解析 parse_tree(root_node, "") 当然,您需要在此功能内部做一些事情,以使其值得做。

如果您在不需要完整路径的地方进行一些解析,只需删除 nkey 可变及其操作,只通过 it->first 递归功能。

答案的补充 如何迭代增压属性树? :

在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->firstnode.first.

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

如果我们想执行一些算法操纵,则可以使用基于BFS的PRING 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;
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top