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 tree using iterators or similar?
In reference there is just an example of browsing the tree through:
BOOST_FOREACH
But is there nothing more? Something like an stl-like container? It would be a better solution, speaking about code quality....
BOOST_FOREACH is just a convenient way for iterating that can be done by iterator, begin() and end()
And in C++11 it's:
Here is what I came up with after much experimentation. I wanted to share it in the community because I couldn't find what I wanted. Everybody seemed to just post the answer from the boost docs, which I found to be insufficient. Anyhow:
Here is the output:
I ran into this issue recently and found the answers incomplete for my need, so I came up with this short and sweet snippet:
Important to note is that any node, except the root node can contain data as well as child nodes. The
if (!key.empty())
bit will get the data for all but the root node, we can also start building the path for the looping of the node's children if any.You'd start the parsing by calling
parse_tree(root_node, "")
and of course you need to do something inside this function to make it worth doing.If you are doing some parsing where you don't need the FULL path, simply remove the
nkey
variable and it's operations, and just passit->first
to the recursive function.BFS based print ptree traversal, May be used if we want to do some algorithmic manipulation