日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于 Boost.PropertyTree

發布時間:2024/4/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于 Boost.PropertyTree 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于 Boost.PropertyTree

Browse: Home / Boost / 關于 Boost.PropertyTree

關于 Boost.PropertyTree

Boost.PropertyTree 應該是 Boost 1.41.0 開始正式加入 Boost 版本的。目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0。


  • 主要作用/應用場合

Boost.PropertyTree 提供了一種結構化的數據存儲容器。并且提供了一系列的解釋器可以將內存中的結構與具體格式相互轉換 (比如: INI, XML, JSON )。

至少可以用在:

  • 進程間通訊或者跨語言的進程間的通訊
  • 一些配置文件的存取
  • 網絡通訊協議的格式

    • 基本用法

    基本用法有 2 種場景。第一種是從 Property Tree存儲到具體格式。第二種是從具體格式解析到具體的 Property Tree。其他還有一些 Property Tree 操作的方法,比如:遍歷、搜索等方法。

    ?

    以下這個 Sample 就是基本用法的測試:

    先把 數據存儲到 datum 中,隨后輸出 相應的 XML 和 JSON 到 std::cout 上。最后再從 JSON Stream 中解析輸入到 ptParse 中獲得相應 的數據。

    #include <stdio.h>#include <iostream> #include <sstream> #include <string> #include <locale>#include "boost/property_tree/ptree.hpp" #include "boost/property_tree/json_parser.hpp" #include "boost/property_tree/xml_parser.hpp"int main(int argc, char **argv) {/* The data format* <root>* <num>1</num>* <str>Test</str>* </root>*/try{/* create the property tree */boost::property_tree::ptree datum;datum.put("root.num", 100);datum.put("root.str", "string");/* output XML string */std::ostringstream xmlOutputStream;boost::property_tree::xml_parser::write_xml(xmlOutputStream,datum);std::cout << "XML format:" << std::endl;std::cout << xmlOutputStream.str() << std::endl;/* output JSON string */std::ostringstream jsonOutputStream;boost::property_tree::json_parser::write_json(jsonOutputStream,datum);std::cout << "JSON format:" << std::endl;std::cout << jsonOutputStream.str() << std::endl;/* read datum from JSON stream */boost::property_tree::ptree ptParse;std::istringstream jsonIStream;jsonIStream.str(jsonOutputStream.str());boost::property_tree::json_parser::read_json(jsonIStream,ptParse);int num = ptParse.get<int>("root.num");std::string strVal = ptParse.get<std::string>("root.str");std::cout << "Num=" << std::dec << num<< " Str=" << strVal << std::endl << std::endl;}catch (...){printf("create boost::property_tree::ptree failed\n");}return 0; }
    • 關于字符集

    Boost 目前是支持 UTF8 的,但是不能用 直接用 Unicode。所以,如果要存儲寬字符就有點麻煩需要用到 Boost 提供的 utf8_codecvt_facet 做轉換。

    下面就是一個存儲 wchar_t 的 Sample:

    和之前的其實差不多,有 2 點主要不同。一是用了 wptree 替換了 ptree。二是增加了 utf8_codecvt_facet 在相應的 Stream 里做轉換。

    ? #include <stdio.h>#include <iostream> #include <sstream> #include <string> #include <locale>#include "boost/property_tree/ptree.hpp" #include "boost/property_tree/json_parser.hpp" #include "boost/property_tree/xml_parser.hpp"#include "boost/program_options/detail/convert.hpp" #include "boost/program_options/detail/utf8_codecvt_facet.hpp"int main(int argc, char **argv) {/* The data format* <root>* <num>1</num>* <str>Test</str>* </root>*//* test UTF-8 format */try{/* create boost utf8 codecvt */std::locale oldLocale;std::locale utf8Locale(oldLocale,new boost::program_options::detail::utf8_codecvt_facet());std::wcout.imbue(utf8Locale);/* create the wptree for save the UTF-8 data */boost::property_tree::wptree datum;datum.put(L"root.num", 100);datum.put(L"root.str", L"wstring");/* output XML string */std::wostringstream xmlOutputStream;xmlOutputStream.imbue(utf8Locale);boost::property_tree::xml_parser::write_xml(xmlOutputStream,datum);std::wcout << L"XML format:" << std::endl;std::wcout << xmlOutputStream.str() << std::endl;/* output JSON string */std::wostringstream jsonOutputStream;jsonOutputStream.imbue(utf8Locale);boost::property_tree::json_parser::write_json(jsonOutputStream,datum);std::wcout << L"JSON format:" << std::endl;std::wcout << jsonOutputStream.str() << std::endl;/* read datum from JSON stream */boost::property_tree::wptree wptParse;std::wistringstream jsonIStream;jsonIStream.imbue(utf8Locale);jsonIStream.str(jsonOutputStream.str());boost::property_tree::json_parser::read_json(jsonIStream,wptParse);int num = wptParse.get<int>(L"root.num");std::wstring wstrVal = wptParse.get<std::wstring>(L"root.str");std::wcout << L"Num=" << std::dec << num<< L" Str=" << wstrVal << std::endl << std::endl;}catch (...){printf("create boost::property_tree::wptree failed\n");}return 0; }
    • 附錄
  • 以上的測試程序,在 Boost 1.42.0 和 MS VS 2008 上測試過。這里是打包文件 PTreeTest
  • 在 Boot.org 上能找到更多的 PropertyTree 的操作。PropertyTree 在 Boost 1.41.0 版本的手冊。最好去看新版本的如果以后更新的話。
  • Boot.PropertyTree 用的 XML 解析器是 RapidXML 。是一個基于模板設計的 XML 操作庫 ,有非常好的性能(據說)。
  • 2 responses to “關于 Boost.PropertyTree”

  • cheating wife January 17, 2013 at 1:21 pm | Permalink

    I realized my girldfriend cheating on me while using various other babe at PlayerBlock

  • small things March 11, 2013 at 6:22 am | Permalink

    I hardly leave a response, but i did some searching and
    wound up here 關于 Boost.PropertyTree. And
    I do have some questions for you if you don’t mind. Could it be just me or does it look as if like some of the responses appear like they are coming from brain dead visitors? And, if you are posting at additional online sites, I would like to keep up with everything fresh you have to post. Would you list of every one of your social sites like your linkedin profile, Facebook page or twitter feed?

  • Leave a Reply

    總結

    以上是生活随笔為你收集整理的关于 Boost.PropertyTree的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。