How to iterate a boost property tree?

前端 未结 5 686
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 03:27

I am know approaching to boost property tree and saw that it is a good feature of boost libs for c++ programming.

Well, I have one doubt? how to iterate a property t

5条回答
  •  囚心锁ツ
    2020-12-24 04:09

    BFS based print ptree traversal, May be used if we want to do some algorithmic manipulation

    int print_ptree_bfs(ptree &tree) {
    try {
        std::queue treeQ;
        std::queue 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<first << " " << itr->second.data() << std::endl;
                }
            }
    
            cout << std::endl;
    
         }
       } catch (std::exception const& ex) {
        cout << ex.what() << std::endl;
       }
       return EXIT_SUCCESS;
      }
    

提交回复
热议问题