property_tree是专为配置文件而写,支持xml,ini和json格式文件
 
ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择。
 
使用property_tree也很简单,boost自带的帮助中有个5分钟指南
 
这里写一下使用xml来保存多维数组,在有些情况下一维数组并不能满足要求。
举个简单的例子吧:
xml格式如下:
  1. <debug>
      <total>3</total>
      <persons>
        <person>
        <age>23</age>
        <name>hugo</name>
      </person>
      <person>
        <age>23</age>
         <name>mayer</name>
      </person>
      <person>
        <age>30</age>
        <name>boy</name>
      </person>
      </persons>
    </debug>
定义结构体如下:
  1. 1 struct person
    2 {
    3 int age;
    4 std::string name;
    5 };
    6
    7 struct debug_persons
    8 {
    9 int itsTotalNumber;
    10 std::vector<person> itsPersons;
    11 void load(const std::string& filename);
    12 void save(const std::string& filename);
    13 };
2个载入和保存的函数:
  1. 1 void debug_persons::save( const std::string& filename )
    2 {
    3 using boost::property_tree::ptree;
    4 ptree pt;
    5
    6 pt.put("debug.total", itsTotalNumber);
    7
    8 BOOST_FOREACH(const person& p,itsPersons)
    9 {
    10 ptree child;
    11 child.put("age",p.age);
    12 child.put("name",p.name);
    13 pt.add_child("debug.persons.person",child);
    14 }
    15 // Write property tree to XML file
    16 write_xml(filename, pt);
    17
    18 }
    19
    20 void debug_persons::load( const std::string& filename )
    21 {
    22 using boost::property_tree::ptree;
    23 ptree pt;
    24
    25 read_xml(filename, pt);
    26 itsTotalNumber = pt.get<int>("debug.total");
    27
    28 BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.persons"))
    29 {
    30 //m_modules.insert(v.second.data());
    31 person p;
    32 p.age = v.second.get<int>("age");
    33 p.name = v.second.get<std::string>("name");
    34 itsPersons.push_back(p);
    35 }
    36 }
main中的代码
  1. 1 int _tmain(int argc, _TCHAR* argv[])
    2 {
    3
    4 try
    5 {
    6   debug_persons dp;
    7   dp.load("debug_persons.xml");
    8   std::cout<<dp<<std::endl;
    9   person p;
    10   p.age = 100;
    11   p.name = "old man";
    12   dp.itsPersons.push_back(p);
    13   dp.save("new.xml");
    14   std::cout << "Success\n";
    15 }
    16 catch (std::exception &e)
    17 {
    18 std::cout << "Error: " << e.what() << "\n";
    19 }
    20 return 0;
    21 }
这里为了调试输出增加了以下代码:
  1. 1 std::ostream& operator<<(std::ostream& o,const debug_persons& dp)
    2 {
    3   o << "totoal:" << dp.itsTotalNumber << "\n";
    4   o << "persons\n";
    5   BOOST_FOREACH(const person& p,dp.itsPersons)
    6   {
    7     o << "\tperson: Age:" << p.age << " Nmae:" << p.name << "\n";
    8   }
    9 return o;
    10 }
ps:需要包含以下文件
  1. 1 #include <boost/property_tree/ptree.hpp>
    2 #include <boost/property_tree/xml_parser.hpp>
    3 #include <boost/foreach.hpp>
    4 #include <vector>
    5 #include <string>
    6 #include <exception>
    7 #include <iostream>

使用boost中的property_tree实现配置文件的更多相关文章

  1. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  2. spring项目中dubbo相关的配置文件出现红叉的问题

    近来在eclipse中导入了一个web项目,但是发现项目上有红色的叉号. 原来是spring中关于dubbo的配置文件报错了. Multiple annotations found at this l ...

  3. .NET平台开源项目速览(20)Newlife.Core中简单灵活的配置文件

    记得5年前开始拼命翻读X组件的源码,特别是XCode,但对Newlife.Core 的东西了解很少,最多只是会用用,而且用到的只是九牛一毛.里面好用的东西太多了. 最近一年时间,零零散散又学了很多,也 ...

  4. git上传中的排除的配置文件, git实际的操作代码;

    git上传中的排除的配置文件: git实际的操作 在主目录建立.gitignore文件并输入以下保存: *.class #package file *.war *.ear #kdiff3 ignore ...

  5. Linux故障:linux中使用ifconfig命令查看网卡信息时显示为eth1,但是在network-scripts中只有ifcfg-eth0的配置文件,并且里面的NAME="eth0"。

    linux中使用ifconfig命令查看网卡信息时显示为eth1,但是在network-scripts中只有ifcfg-eth0的配置文件,并且里面的NAME="eth0".   ...

  6. vue-cli脚手架build目录中的build.js配置文件

    该随笔收藏自: vue-cli脚手架build目录中的build.js配置文件 这个配置文件是命令npm run build 的入口配置文件,主要用于生产环境 由于这是一个系统的配置文件,将涉及很多的 ...

  7. Boost中的智能指针(转)

    这篇文章主要介绍 boost中的智能指针的使用.(转自:http://www.cnblogs.com/sld666666/archive/2010/12/16/1908265.html) 内存管理是一 ...

  8. nginx指令中的优化(配置文件)

    nginx指令中的优化(配置文件)worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数.worker_cpu_affinity 00000001 0000 ...

  9. Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别

    Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别 一.Web.xml中自动扫描Spring的配置文件(applicationCont ...

随机推荐

  1. GCC -Wall

    官网:http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Warning-Options.html#Warning-Options3.8 Options to Re ...

  2. Python web框架有哪些

    简单易学的web.py, 大型的django:文档最完善.市场占有率最高.招聘职位最多. Tornado 具体看:http://feilong.me/2011/01/talk-about-python ...

  3. 基于PCA的人脸识别步骤

    代码下载:基于PCA(主成分分析)的人脸识别 人脸识别是一个有监督学习过程,首先利用训练集构造一个人脸模型,然后将测试集与训练集进行匹配,找到与之对应的训练集头像.最容易的方式是直接利用欧式距离计算测 ...

  4. 用PHP编写Hadoop的MapReduce程序

    用PHP编写Hadoop的MapReduce程序     Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...

  5. HDU 5281 Senior&#39;s Gun

    Senior's Gun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  6. Android短彩信源码解析-短信发送流程(二)

    转载请注明出处:http://blog.csdn.net/droyon/article/details/11699935 2,短彩信发送framework逻辑 短信在SmsSingleRecipien ...

  7. reason: 'unable to dequeue a cell with identifier Cell

    今天在cell重用的时候出现一下错误 reason:  'unable  to  dequeue  a  cell  with  identifier  Cell  -  must  register ...

  8. c++ 虚析构函数[避免内存泄漏]

    c++  虚析构函数: 虚析构函数(1)虚析构函数即:定义声明析构函数前加virtual 修饰, 如果将基类的析构函数声明为虚析构函数时,由该基类所派生的所有派生类的析构函数也都自动成为虚析构函数. ...

  9. [Swust OJ 322]--东6宿舍灵异事件(中缀表达式转化为后缀表达式的简单运用)

    题目链接:http://acm.swust.edu.cn/problem/322/ Time limit(ms): 1000 Memory limit(kb): 65535     Descripti ...

  10. BZOJ 1597: [Usaco2008 Mar]土地购买( dp + 斜率优化 )

    既然每块都要买, 那么一块土地被另一块包含就可以不考虑. 先按长排序, 去掉不考虑的土地, 剩下的土地长x递增, 宽y递减 dp(v) = min{ dp(p)+xv*yp+1 } 假设dp(v)由i ...