How to Access Data in a property Tree

Source: Internet
Author: User
Tags value of pi

How do I access the data in the attribute tree?

The property tree is similar to (almost) a standard container with a value type of pair. It has the usual member functions, such as INSERT, push_back, find, erase, and so on, which can of course be used to populate and access the tree. For example, the following code adds data with the keyword "PI" (almost) equal to the mathematical pi value:

 
   
  
  1. ptree pt;
  2. pt.push_back(ptree::value_type("pi", ptree("3.14159")));
In order to find the value of pi, we can do this:
 
   
  
  1. ptree::const_iterator it = pt.find("pi");
  2. double pi = boost::lexical_cast<double>(it->second.data());
Here is also a hint: here is a very useful extension type conversion, convert library function:Lexical_cast
This looks like a hassle, and if the pi value isn't stored near the top of the tree, we'll be more concerned about the error. Fortunately, there is another right way:
 
   
  
  1. ptree pt
  2. pt put ( "PI" Span class= "lit" >3.14159 //put double
  3. double pi = PT get <DOUBLE> "PI" //get double
Nothing is more simple than that. Basically, there are 2 family member functions, get and put, which allow you to visually access the data stored in the tree (either directly to the child or not).
Three ways to get data
Get has three versions: GET, get (default version), and get_optional, which are different from failed processing policies. All versions use the path specifier, which determines which key to search for values. It can be a single key or a path to a key, where the path element is in a special character (.). Separated. (if not specified). For example Debug.logging.errorlevelMay be a valid path with a dot as a delimiter.
1. Handling of throwing exceptions
 
   
  
  1. ptree pt;
  2. /* ... */
  3. float v = pt.get<float>("a.path.to.float.value");

This call locates the appropriate node in the tree and attempts to convert its data string to a floating-point value. If it fails, an exception is thrown. If the path does not exist, it will be an Ptree_bad_path exception. If the value cannot be translated, it will be ptree_bad_data. They all originate from ptree_error, so that ordinary processing can be made possible.
2. Versions with default values
 
   
  
  1. ptree pt;
  2. /* ... */
  3. float v = pt.get("a.path.to.float.value", -1.f);
It will perform the same operation as above, but if it fails, it will return the default value specified by the second parameter (here -1.f) instead of throwing it. This is very useful under normal circumstances, because people want to omit some keys. Note that there is usually no necessary type specification here, because the type is determined by the default value parameter.
3. Optional version
 
   
  
  1. ptree pt;
  2. /* ... */
  3. boost::optional<float> v = pt.get_optional<float>("a.path.to.float.value");
This version usesBoost::Optionalto handle the fetch failure. On a successful fetch, it will returnBoost::Optionalthe initialization value. Otherwise, it returns the uninitializedBoost::Optional.
To retrieve values from this tree (not some sub-keys), you can use Get_value, Get_value (Default-value version), and get_value_optional. They have the same semantics to get functions, but they do not accept path parameters. Do not call the get and empty paths, because it will attempt to extract the contents of the subkey with an empty name.

If you want to use a separation other than the default, you need to explicitly construct the path object. PThe path type of the tree is String_path instantiated, so the easiest way to reference it is Ptree::p ath_type. so you can use it on their key to have (.) The tree "is a bit of a path in key":
  
 
  1. typedef ptree::path_type path;
  2. pt.get<float>(path("p.a.t.h/t.o/v.a.l.u.e", ‘/‘));
  3. pt.get(path("p.a.t.h/t.o/v.a.l.u.e", ‘/‘), 0, NULL);
  4. pt.get_optional<std::string>(path("p.a.t.h/t.o/v.a.l.u.e", ‘/‘));
As you can see, here is the "/" to use as a delimiter, the reason, very simple, you have a point in the key (.), you can no longer use points as a separate, we need to use the new characters!
Note: The special overloads of Get and get_optional that exist in the pre-release version of Propertytree have been removed. This is because overloading conflicts with data converters that use each call.

Ways of putting data for the same reason, there is a value added, there is a value! Get and put are a pair,you do not need to process the missing values when adding data. If the supplied value cannot be converted to the data type of the tree, the function throws Ptree_bad_data
  
 
  1. ptree pt;
  2. pt.put("a.path.to.float.value", 3.14f);
  3. // Overwrites the value
  4. pt.put("a.path.to.float.value", 2.72f);
  5. // Adds a second node with the new value.
  6. pt.add("a.path.to.float.value", 3.14f);

Calling put inserts a new value in the specified path so that the call to specify the same path will retrieve it. In addition, the put inserts any missing path elements during path traversal. For example, call the put("Key1.key2.key3", 3.14f),On an empty tree,three new children will be inserted:Key1,Key1.Key2;Key1.key2.key3. The last one will receive a string "3.14" as the data, and the first two will have an empty data string. Always insert a new key after the existing sequence
the difference between put and add is that the put overwrites the existing value, and if so, add creates a new node to hold the value, even if the specified path references an existing nodesimilar to Get_value, there is also a put_value function. It does not accept the path:
ptreePT ; PT .put_value (3.14f);



Null

How to Access Data in a property Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.