Property tree resembles (almost is) a standard container with value type of
It has the usual member functions, such as
etc. These can of course be used to populate and access the tree. For example
the following code adds key
with data (almost) equal to mathematical pi value:
To find the value of
might do the following:
const_iteratorit = pt.
find("pi"); double pi = boost::lexical_cast<double>(it->second.
This looks quite cumbersome, and would be even more so if
value was not stored so near the top of the tree, and we cared just a little
bit more about errors. Fortunately, there is another, correct way of doing
ptree pt; pt.
put("pi", 3.14159); // put double double pi = pt.
get<double>("pi"); // get double
It doesn't get simpler than that. Basically, there are 2 families of member
which allow intuitive access to data stored in the tree (direct children or
There are three versions of get: get, get (default-value version), and get_optional, which differ by failure handling strategy. All versions take path specifier, which determines in which key to search for a value. It can be a single key, or a path to key, where path elements are separated with a special character (a '.' if not specified differently). For example debug.logging.errorlevel might be a valid path with dot as a separator.
The throwing version (
ptreept; /* ... */ float v = pt.
This call locates the proper node in the tree and tries to translate its
data string to a float value. If that fails, exception is thrown. If path
does not exist, it will be
exception. If value could not be translated, it will be
Both of them derive from
to make common handling possible.
The default-value version (
ptreept; /* ... */ float v = pt.
It will do the same as above, but if it fails, it will return the default value specified by second parameter (here -1.f) instead of throwing. This is very useful in common situations where one wants to allow omitting of some keys. Note that type specification needed in throwing version is normally not necessary here, because type is determined by the default value parameter.
The optional version (
ptreept; /* ... */ boost::optional<float> v = pt.
This version uses boost::optional class to handle extraction failure. On successful extraction, it will return boost::optional initialized with extracted value. Otherwise, it will return uninitialized boost::optional.
To retrieve a value from this tree (not some subkey), use
(default-value version), and
They have identical semantics to
functions, except they don't take the
parameter. Don't call
with and empty
to do this as it will try to extract contents of subkey with empty name.
To use a separator character other than default '
need to construct a path object explicitly. The path type for a
ptree is a string_path instantiation,
so the easiest way to refer to it is
This way you can use trees that have dots in their keys:
typedef ptree::path_type path; pt.get<float>(path("p.a.t.h/t.o/v.a.l.u.e", '/')); pt.get(path("p.a.t.h/t.o/v.a.l.u.e", '/'), 0, NULL); pt.get_optional<std::string>(path("p.a.t.h/t.o/v.a.l.u.e", '/'));
Note: the special overloads of
taking a separator character that existed in pre-release versions of PropertyTree
have been removed. This is because the overloads conflicted with using per-call
they have only one variant each. This is because there is no need to deal with
missing values when adding data. If the supplied value cannot be converted
to the tree's data type, the functions will throw
put("a.path.to.float.value", 3.14f); // Overwrites the value pt.
put("a.path.to.float.value", 2.72f); // Adds a second node with the new value. pt.
will insert a new value at specified path, so that a call to
specifying the same path will retrieve it. Further,
will insert any missing path elements during path traversal. For example, calling
on an empty tree will insert three new children:
key1.key2.key3. The last one will receive a string
"3.14" as data, while
the two former ones will have empty data strings.
always inserts new keys at the back of the existing sequences. The difference
is that put will overwrite existing values if there are any, while add will
create a new node to hold the value even if the specified path references an
there is also a
function. It does the same for this property tree what
does for its children. Thus, it does not receive a
There is no add_value function.