boost propertyTree
Boost PropertyTree provides a tree structure to store key/value pairs. Tree structures means that a trunk exists with numerous branches that have numerous twigs. A file system is a good example of a tree structure. File systems have a root directory with subdirectories that themselves can have subdirectories and so on.
1. accessing data in boost::property_tree::ptree
#include <boost/property_tree/ptree.hpp>
#include <iostream> using boost::property_tree::ptree; int main() {
ptree pt;
pt.put("C:.Windows.System", "20 files"); ptree& c = pt.get_child("C:");
ptree& windows = c.get_child("Windows");
ptree& system = windows.get_child("System");
std::cout << system.get_value<std::string>() << std::endl;
return ;
}
put() expects two parameters because boost::property_tree::ptree is a tree structure that saves key/value pairs. The tree doesn't just consist of branches and twigs, a value must be assigned to each branch and twig.
The first parameter passed to put() is more interesting. It is a path to a directory. However, it doesn't use the backslash, which is the common path separator on Windows. It uses the dot.
To access a subbranch, you call get_child(), which returns a reference to an object of the same type get_child() was called on.
2. accessing data in basic_ptree<std::string, int>
#include <boost/property_tree/ptree.hpp>
#include <utility>
#include <iostream> int main() {
typedef boost::property_tree::basic_ptree<std::string, int> ptree;
ptree pt;
pt.put(ptree::path_type{"C:\\Windows\\System", '\\'}, );
pt.put(ptree::path_type{"C:\\Windows\\Cursors", '\\'}, ); ptree& windows = pt.get_child(ptree::path_type{"C:\\Windows"}, '\\');
int files = ;
for (const std::pair<std::string, ptree>& p : windows) {
files += p.second.get_value<int>();
}
std::cout << files << std::endl;
return ;
}
By default, Boost.PropertyTree uses a dot as the separator for keys. If you need to use another character, such as backslash, as the separator, you don't pass the key as a string to put(). Instand you wrap it in an object of type boost::property_tree::ptree::path_type. The constructor of this class, which depends on boost::property_tree::ptree, takes the key as its first parameter and the separator as its second parameter.
3. accessing data with a translator
#include <boost/property_tree/ptree.hpp>
#include <boost/optional.hpp>
#include <cstdlib> struct string_to_int_translator {
typedef std::string internal_type;
typedef int external_type; boost::optional<int> get_value(const std::string& s) {
char* c;
long l = std::strtol(s.c_str(), &c, );
return boost::make_optional(c != s.c_str(), static_cast<int>());
}
}; int main() {
typedef boost::property_tree::iptree ptree;
ptree pt;
pt.put(ptree::path_type{"C:\\Windows\\System", '\\'}, "20 fifles");
pt.put(ptree::path_type{"C:\\Windows\\"}, "50 files"); string_to_int_translator tr;
int files = pt.get<int>(ptree::path_type{"C:\\windows\\system", '\\'}, tr) + pt.get<int>(ptree::path_type{"C:\\windows\\cursors", '\\'}, tr);
std::cout << files << std::endl;
return ;
}
boost::property_tree::iptree doesn't distinguish between lower and upper case. Just as put() can be used to store a value in a subbranch directly, a value from a subbranch can be read with get(). The key is defined the same way---using boost::property_tree::iptree::path_type.
Like get_value(), get() is a function template. You have to pass the type of the return value as a template parameter. Boost.PropertyTree does an automatic type conversion.
string_to_int_translator converts a value of type std::string to int. The translator is passed as an additional parameter to get(). Because the translator is just used to read, it only defines one member function, get_value(). If you want to use the translator for writing, too, then you would need to define a member function put_value() and then pass the translator as an additional parameter to put().
4. various member functions of boost::property_tree::ptree
#include <boost/property_tree/ptree.hpp>
#include <utility>
#include <iostream> using boost::property_tree::ptree; int main() {
ptree pt;
pt.put("C:.Windows.System", "20 files"); boost::optional<std::string> c = pt.get_optional<std::string>("C:");
std::cout << std::boolalpha << c.is_initialized() << std::endl; pt.put_child("D:.Program Files", ptree{"50 files"});
pt.put_child("D:.Program Files", ptree{"60 files"}); ptree d = pt.get_child("D:");
for (const std::pair<std::string, ptree>& p : d) {
std::cout << p.second.get_value<std::string>() << std::endl;
} boost::optional<ptree&> e = pt.get_child_optional("E:");
std::cout << e.is_intialized() << std::endl;
return ;
}
You can call the member function get_optional() if you want to read the value of a key, but you aren't sure if the key exists. get_optional() returns the value in an object of type boost::optional. The object is empty if the key wasn't found. Otherwise, get_optional() works the same as get().
The difference between put_child() and add_child() is that put_child() accesses a key if that key already exists, while add_child() always inserts a new key into the tree. get_child_optional() is used like get_child(). get_child_optional() returns an object of type boost::optional.
5. serializing a boost::property_tree::ptree in the JSON format
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream> using namespace boost::property_tree; int main() {
ptree pt;
pt.put("C:.Windows.System", "20 files");
pt.put("C:.Windows.Cursors", "50 files"); json_parser::write_json("file.json", pt); ptree pt2;
json_parser::read_json("file.json", pt2); std::cout << std::boolalpha << (pt == pt2) << std::endl;
return ;
}
write_json() and read_json() make it possible to save and load a boost::property_tree::ptree serialized in the JSON format. That way you can support configuration files in the JSON format.
Except json format, Boost.PropertyTree supports additional data formats. you use boost::property_tree::ini_parser::write_ini() adn boost::property_tree::ini_parser::read_ini() to supprot ini-files; boost::property_tree::xml_parser::write_xml() and boost::property_tree::xml_parser::read_xml to support xml format.
boost propertyTree的更多相关文章
- 使用Boost.PropertyTree处理XML、JSON和INI数据
Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的.目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0. 主要作用/应用场合 B ...
- Boost.PropertyTree读取ini文件(Linux环境)
昨天因为需要读取配置文件略略伤神.网上很多例子但是我用都会报错,很多人把Boost.PropertyTree的函数写很麻烦的包所以报错我也不知道什么问题,所以今天整理下. 头上附上官网链接:Boost ...
- Boost.JSON Boost的JSON解析库(1.75首发)
目录 目录 Boost的1.75版本新库 JSON库简介 JSON的简单使用 编码 最通用的方法 使用std::initializer_list json对象的输出 两种对比 解码 简单的解码 增加错 ...
- Awesome C/C++
Awesome C/C++ A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. In ...
- awesome cpp
https://github.com/fffaraz/awesome-cpp Awesome C/C++ A curated list of awesome C/C++ frameworks, lib ...
- C/C++ 开源库及示例代码
C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...
- TinyXML2的快速实践
最近遇到个需要在C++中处理XML文件的需求,虽然对此方面并不是很熟,但好在有GitHub上的awesome-cpp项目的帮助,还是收获了足够的相关知识. 类库 常用的或被推荐的XML类库有以下数个选 ...
- [转]awsome c++
原文链接 Awesome C++ A curated list of awesome C++ (or C) frameworks, libraries, resources, and shiny th ...
- 基于BOOST 实现并发服务器框架
一:设计思路 本服务器框架使用 UDP 传输协议,程序柱线程等待客户端数据,并将数组存取队列缓冲区.另外可开启多个工作线程,工作线程可以依据具体项目实现不同的功能 ,例如可以将队列缓冲区中的数据逐个取 ...
随机推荐
- 简易的Web自动化链接测试(Xenu)
1.理解链接需要测试的测试点: [1] 要测试的链接页面是否存在 [2] 确定存在链接页面,然后就考虑跳转后的页面是不是对应需求的页面[3] 保证Web系统上没有孤立的页面(没有链接指向该页面) 2. ...
- JS-立即执行函数表达式(IIFE)
javascript 函数调用 在 javascript 中,每一个函数在被调用的时候都会创建一个执行上下文,在该函数内部定义的变量和函数只能在该函数内部被使用,而正是因为这个上下文,使得我们在调用函 ...
- 【转】PyQt5系列教程(七)控件
PyQt5系列教程(七)控件 软硬件环境 Windows 10 Python 3.4.2 PyQt 5.5.1 PyCharm 5.0.4 前言 控件是PyQt应用程序的基石.PyQt5自带很多不 ...
- Linux下复杂PC问题——多进程编程/信号量通信/共享存储区
进程相关函数 pid_t fork(); 头文件:unistd.h,sys/types.h 作用:建立一个新进程(子进程),子进程与原进程(父进程)共享代码段,并拥有父进程的其他资源(数据.堆栈等)的 ...
- JS动态添加Easyui的HTML时样式丢失
解决办法: $.parser.parse($("#creatLi").html(<li>xxxxxx</li>)); ------------------- ...
- 【报错】An error happened during template parsing (template: "class path resource [templates/hello1.html]")
页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...
- python字典、字符串(json串)、字节串之间的转化
字典和json字符串(本质也是字符串)之间的转化用json.dumps和json.loads() json.dumps(): 字典→json字符串 json.loads(): json字符 ...
- CentOS下编译Lua使得其支持动态链接
在Linux下编译Lua时,我一般都是使用的make generic,这样编译没有什么问题,运行lua的程序也都OK,但是,这样在加载外部的C动态 链接库,却总是报下面的错误 dynamic libr ...
- 最小生成树(prim和Kruskal操!!SB题)
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30571 Accepted: 9220 D ...
- idea 配置maven web项目
文章转自:https://www.cnblogs.com/weiqingfeng/p/9494914.html 步骤一:首先先创建一个project,上次我说过了创建一个project就是一个工作空间 ...