boost庫總結一

progress_display

在控制檯顯示程序的執行進度,但是無法把進度顯示輸出與程序的輸出分離。

#include<boost/progress.hpp>
using namespace boost;
progress_display pd(long expected_count);
//增加進度
pd++;

date_time

date_time庫需要編譯。
date_time庫的日期基於格里高利曆,位於命名空間boost::gregorian。

#include <boost/date_time/gregorian/gregorian.hpp>
using namespace boost;
gregorian::date mydate(2015,12,20);
//轉換爲字符串
gregorian::to_simple_string(mydate);
gregorian::to_iso_string(mydate);

date對象可以比較,==、>、<等。date對象也可以從字符串構造:

date d1=from_string(“2015-12-20”);
date d2=from_string(“2015/12/20”);
//返回當前的日期對象
gregorian::day_clock::local_day();
//訪問日期對象
date.year() date.month() date.day() date.day_of_week() date.day_of_year() date.week_number()
date.is_special()

日期迭代器包括date_iterator、week_iterator、month_iterator、year_iterator,分別以天、周、月和年爲單位遞減,構造時傳入一個起始日期和步長,默認是1個單位。

day_iterator d_iter(date);
d_iter++;

property_tree

property_tree是一個保存了多個屬性值的樹形數據結構,可以解析xml、ini、json、info四種格式的文本數據。
property_tree位於boost::property_tree。

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp>

首先讀取xml配置文件

using namespace boost::property_tree;
ptree pt;
read_xml(“conf.xml”,pt);

使用get<type>()通過路徑訪問屬性樹內的節點

pt.get<string>(“conf.theme”);
//指定缺省值
pt.get(“conf.number”,100);

lexical_cast數據類型轉換

boost::regex的默認正則表達式是perl語法。
如果轉換失敗,boost::lexical_cast會拋出一個bad_lexical_cast異常。

#include <iostream>
#include <boost/lexical_cast.hpp>
   string s=”123”;
   int a=lexical_cast<int>(s);
   try{
     int c=lexical_cast<int>(“wrong number”);
   }
  catch(bad_lexical_cast &e){
   cout<<e.what()<<endl;
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章